I want to create a function in zshrc for the following command -
node scripts/node_es6.js scripts/small_run_oneoff.js runMiaEventsStatsJob '{"targetDate": "02-01-2018"}'
I want to pass the targetDate as a command line argument. So, I wrote the following function in zshrc -
function mia-events-stats() {
node scripts/node_es6.js scripts/small_run_oneoff.js runMiaEventsStatsJob '{"targetDate": "$1"}'
}
This however does not work. When I execute mia-events-stats 02-01-2018, the targetDate passed to the actual running code is $1.
What am I missing here?
Characters of a string inside single quotes is quoted. Thus, your dollar sign is read as a normal character.
You should replace your single quotes by doubles quotes to let the magic happen, and escape inner double quotes like that:
"{\"targetDate\": \"$1\"}"
If you need your single quotes to be read, simply add them:
"'{\"targetDate\": \"$1\"}'"
Single quotes won't have any effect thanks to doubles quotes.
Related
This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed last year.
I have a variable:
my_var="$('/some/path/to/username/more/path' 'and/something/else')"
and I want to change it to be more generic like:
my_var="$('/some/path/to/${USER}/more/path' 'and/something/else')"
but that doesn't work. How can I get the user inside of there?
The problem is that inside a single quoted string nothing is treated with a special meaning. This makes them good for scripting, eg. grep '^pattern$' ... and sed 's/abc$/def/'.
You can use double quotes instead.
$(...) is a command substitution. Is that correct? If so, then you can nest double qoutes:
my_var="$("/some/path/to/${USER}/more/path" 'and/something/else')"
This should be interpreted as two set of quotes, so the outer double quotes, isn't stopped at the first inner quote:
my_var="$( )" # First set of quotes
"/some/path/to/${USER}/more/path" # Second set of quotes
When assigning a variable, you don't need to wrap the expression in double quotes, so:
my_var=$("/some/path/to/${USER}/more/path" 'and/something/else')
would also be fine in this case. Most other cases you should always wrap parameter expansions ($abc), command substitutions ($(...)) etc. in double quotes as to avoid word splitting and pathname expansions.
However to me it seems like you are trying to create an array instead?
$ my_var=("/some/path/to/${USER}/more/path" 'and/something/else')
$ echo "${my_var[0]}"
/some/path/to/and/more/path
Hello I'm reading a book about bash scripting and the author says to add the following to the end of my .bashrc file. export PATH=~/bin:"$PATH" in order to execute my file from the command line by typing its name. I notice however that if I put export PATH=~/bin:$PATH I can achieve the same result. So my question is what is the difference between the one with quotes and the one without quotes? thanks.
The quotes won't hurt anything, but neither are they necessary. Assignments are processed specially by the shell. From the man page:
A variable may be assigned to by a statement of the form
name=[value]
If value is not given, the variable is assigned the null string. All values undergo tilde expansion, parameter and variable
expansion, command substitution, arithmetic expansion, and
quote removal (see EXPANSION below).
Notice that word-splitting and pathname generation are not on the list in bold. These are the two types of expansion you are trying to prevent by quoting a parameter expansion, but in this context they are not performed. The same rules apply to the assignments that are passed to the export built-in command.
You must include the variable PATH inside double quotes. So that it would handle the filepaths which has spaces but without double quotes, it won't handle the filenames which has spaces in it.
I was facing the same with trying to assign a JSON string to a variable in the terminal.
Wrap it with Single Quotes or Double Quotes
Use single quotes, if you string contains double quotes and vice-versa.
$ export TEMP_ENV='I like the "London" bridge'
$ echo $TEMP_ENV
>> I like the "London" bridge
$ export TEMP_ENV="I like the 'London' bridge"
$ echo $TEMP_ENV
>> I like the 'London' bridge
I am trying to set the following string as a variable in a bash script and am getting some errors. I assume that it is because I need to use quotations or escape it etc.
VARIABLENAME=$([(A"sometest",SomeOtherText "MoreText"),(A"sometext",SomeOtherText 100),(A"Sometext,SomeOtherText "SomeText")]}))
This doesn't work when I try to set it.
The text inside $(...) will be interpreted as a command to run. I believe you want this instead:
VARIABLENAME='[(A"sometest",SomeOtherText "MoreText"),(A"sometext",SomeOtherText 100),(A"Sometext,SomeOtherText "SomeText")]})'
Use single quotes around your string, as it contains double quotes and does not contain any variables to expand.
One error is near the end:
"Sometext,
There is an unclosed ".
A="echo 'q'"
$A
I got the result 'q'
but if I type echo 'q' directly, it's q (without single quote)
So, I wonder what rule does bash follow while facing single quotes inside the double quotes.
The original problem is
A="curl http://123.196.125.62/send -H 'Host: poj.org' -e http://poj.org/send"
$A
I got curl: (6) Couldn't resolve host 'poj.org''
it will be all right if I just type the command into the terminal..
P.S. I'd like to use $A for excuting the command inside A
Please see BashFAQ/050: I'm trying to put a command in a variable, but the complex cases always fail!
It's best to avoid putting commands in variables for the reason you've experienced, among others.
You should use a function and pass it arguments.
Why do you want to do this rather than simply executing the command directly?
If you must do it, use an array:
A=(curl http://123.196.125.62/send -H 'Host: poj.org' -e http://poj.org/send)
${A[#]}
Regarding the treatment of single quotes within double quotes, they are treated literally and as part of the rest of the string. Here is the relevant paragraph from man bash:
Enclosing characters in double quotes preserves the literal value of
all characters within the quotes, with the exception of $, `, \, and,
when history expansion is enabled, !. The characters $ and ` retain
their special meaning within double quotes. The backslash retains its
special meaning only when followed by one of the following characters:
$, `, ", \, or <newline>. A double quote may be quoted within double
quotes by preceding it with a backslash. If enabled, history expansion
will be performed unless an ! appearing in double quotes is escaped
using a backslash. The backslash preceding the ! is not removed.
If you want to "save" a command for later execution, you do NOT want a variable. You want a function.
a() { curl http://123.196.125.62/send -H 'Host: poj.org' -e http://poj.org/send; }
Putting code in variables is bad since variables are containers for data not code. Additionally, you are seeing the problem because $A does NOT execute the bash code in A, what it really does is split the value of A into words, then it performs Pathname Expansion on these words, and as a result of those two operations, it executes a program named by the first resulting word and passes the other words as arguments. In your particular case, this is what happens (I use [] to indicate "units"):
A: [echo 'q']
after wordsplitting: [echo] ['q']
after pathname expansion: [echo] ['q']
Now bash looks for a program called echo and passes the argument 'q' to it.
This is NOT executing bash code, because if you execute echo 'q' as bash code, bash removes the single quotes after it's done with them. Similarly, you cannot do pipes, redirection et al. like this, because they too are bash syntax (just like your single quotes).
Recap: never put code in bash variables. never leave parameters unquoted (if you think doing that fixes something, you are wrong, you've just made things worse, go fix the real problem). The solution is to use a function.
You'd better use backquotes in this case:
A=`curl http://123.196.125.62/send -H 'Host: poj.org' -e 'http://poj.org/send'`
I am trying to send a query to SQLite from the command line using bash. I need to escape both single quotes and double quotes, and escape them so that bash does not misinterpret them. Here is a typical query:
select * from contacts where source = "Nancy's notes";
How can I send this query from the command line? The basic syntax is something like this:
sqlite3.bin contacts.db 'select * from contacts where source = "Nancy's notes"'
But in this case, the shell misinterprets either the single or double quotes. I've tried escaping using both double and triple slashes but this doesn't work. I'm befuddled. Any suggestions?
The trouble with MarkusQ's solution is knowing which characters are special inside double quotes - there are quite a lot of them, including back-ticks, dollar-open parenthesis, dollar-variable, etc.
I would suggest it is better to enclose the string inside single quotes; then, each single quote inside the string needs to be replaced by the sequence quote, backslash, quote, quote:
sqlite3.bin contacts.db 'select * from contacts
where source = "Nancy'\''s notes"'
The first quote in the replacement terminates the current single-quoted string; the backslash-quote represents a literal single quote, and the final quote starts a new single-quoted string. Further, this works with Bourne, Korn, Bash and POSIX shells in general. (C Shell and derivatives have more complex rules needing backslashes to escape newlines, and so on.)
If bash is your only problem, enclose the whole thing in double quotes and then escape anything that's special within bash double quotes with a single backslash. E.g.:
sqlite3.bin contacts.db "select * from contacts where source = \"Nancy's notes on making \$\$\$\""
Here I use two single quotes that sqlite interprets as one.
sqlite3.bin contacts.db "select * from contacts where source = 'Nancy''s notes on making \$\$\$'"
foo_double_single_quote=`echo ${foo_with_single_quote} | sed "s/\'/\'\'/"g`
sqlite3 "INSERT INTO bar_table (baz_colname) VALUES ('${foo_double_single_quote}');"
if you have a variable foo_with_single_quote whose contents you want to insert into a DB, you can use sed like so to create a variable that has the ' character duplicated as necessary.