user6568026
user6568026

Reputation:

Quoting arguments with literal $s in bash scripts

I'm trying to use a variable in a script to store a string that contains $s, and to pass that string unmodified as part of a command whose usage specifies that its argument should be in single quotes.

This works perfectly:

./mycommand -a 1 --arg='$a$a'

In the script, I'm trying to write it as follows:

args='$a$a'
./mycommand -a 1 --arg="'$args'"

The command doesn't accept the syntax it's passed as valid in this case. What could be wrong?


Additional edit: this script is to run hashcat, the password cracking program. I believe that it requires the single quotes for --left-rule='$a$a' and --right-rule='$b$b', especially if there are any spaces in the rule.

Upvotes: 0

Views: 1298

Answers (1)

chepner
chepner

Reputation: 531908

The single quotes inside the double quotes do not prevent expansion; they are literal characters. Compare:

$ a=5
$ echo "'$a$a'"
'55'

Since you just want to pass the literal string $a$a as an argument, it is sufficient to quote the parameter assignment; no additional single quotes are necessary:

args='$a$a'   # The literal string $a$a
./command -a $iter --arg="$args"  # $args expands to $a$a; no further expansion is attempted.

Upvotes: 1

Related Questions