292
votes
Accepted
How can we run a command stored in a variable?
This has been discussed in a number of questions on unix.SE, I'll try to collect all issues I can come up with here. Below is
a description of why and how the various attempts fail,
a way to do it ...
91
votes
What is the difference between the "...", '...', $'...', and $"..." quotes in the shell?
All of these mean something different, and you can write different things inside them (or the same things, with different meaning). Different kinds of quote interpret different escape sequences inside ...
79
votes
Accepted
What's a good mnemonic for shell double vs. single quotes?
Single quotes are simple quotes, with a single standard: every character is literal.
Double quotes have a double standard: some characters are literal, others are still interpreted unless there's a ...
63
votes
Accepted
awk print apostrophe/single quote
Here are a couple of ways:
use octal escape sequences. On ASCII-based systems where ' is encoded as byte 39 (octal 047), that would be:
awk '{print "\047" $0 "\047"}' input
'...
58
votes
How to quote arguments with xargs
I had a similar requirement and ended up using the -I switch to have a placeholder and I was able to quote it.
find . -size +1M | xargs -I {} echo "{}" # just for show (ha)
find . -size +1M ...
53
votes
Accepted
What's the right way to quote $(command $arg)?
You need to use "$(somecmd "$file")".
Without the quotes, a path with a space will be split in the argument to somecmd, and it will target the wrong file. So you need quotes on the inside.
Any ...
47
votes
Accepted
Why doesn't alias foo='echo "This is a quote: \'"' work?
This is zsh, man, not fish.
In zsh, like in every Bourne-like shell (and also csh), single quotes are strong quotes, there is no escaping within them (except by using the rcquotes options as hinted by ...
39
votes
Why use double quotes in a [[ ]] test?
You don't actually need the quotes here. This is one of the very few cases where it is safe to use a variable unquoted. You can confirm this with set -x:
$ var1=""
$ var2="3"
$ set -x
$ if [[ $var1 -...
34
votes
Why do options in a quoted variable fail, but work when unquoted?
The most robust way to code that is to use an array:
wget_options=(
--mirror
--no-host-directories
--directory_prefix="$1"
)
wget "${wget_options[@]}" "$2/$3"
32
votes
Accepted
Is there a command to write text to a file without redirection, pipe or function?
This is a bit of an XY problem but fortunately you've explained your real problem so it's possible to give a meaningful answer.
Sure, there are commands that can write text to a file without relying ...
31
votes
Accepted
How do ssh remote command line arguments get parsed
There is always a remote shell. In the SSH protocol, the client sends the server a string to execute. The SSH command line client takes its command line arguments and concatenates them with a space ...
28
votes
Accepted
Why do options in a quoted variable fail, but work when unquoted?
Basically, you should double quote variable expansions to protect them from word splitting (and filename generation). However, in your example,
wget_options='--mirror --no-host-directories'
wget $...
28
votes
Is it dangerous to run echo without quotes?
Just an extra note on top of @Kusalananda's fine answer.
echo run after_bundle
is fine because none of the characters in those 3 arguments¹ passed to echo contain characters that are special to the ...
23
votes
Accepted
How Can I Expand A Tilde ~ As Part Of A Variable?
The POSIX standard imposes word expansion to be done in the following order (emphasize is mine):
Tilde expansion (see Tilde Expansion), parameter expansion (see Parameter Expansion), command ...
23
votes
Accepted
Is it dangerous to run echo without quotes?
For the specific case
echo run after_bundle
quoting is not needed. No quoting is needed because the argument to echo are static strings that contain no variable expansions or command substitutions ...
23
votes
How can we run a command stored in a variable?
The safest way to run a (non-trivial) command is eval. Then you can write the command as you would do on the command line and it is executed exactly as if you had just entered it. But you have to ...
22
votes
Accepted
Script not working when passed to bash via -c
In your one-liner, the $1 is within a double quoted string:
"cat <(date|awk '{print $1}')"
This means that it will be expanded by the interactive shell. If the first positional parameter, $1, is ...
22
votes
Accepted
What are curly quotes and can I use them in my code?
Curly quotes, or “smart quotes” are used in typography and "straight quotes" will often be automatically changed to curly quotes by various word processing programs. They are sometimes ...
21
votes
How can I use column to delimit on tabs and not spaces?
For POSIX, $'...', as known as ANSI-C escaping, isn't defined.
Instead, you can use the POSIX $(printf '\t'):
column -t -s "$(printf '\t')"
$(printf '\011') could be used, as 011 (octal ...
21
votes
Accepted
Environment Variables with $ Character
$ export CLOUD_PASSWORD='Pass4Aa$ditya'
$ printf '%s\n' "$CLOUD_PASSWORD"
Pass4Aa$ditya
$ export CLOUD_PASSWORD="Pass4Aa\$ditya"
$ printf '%s\n' "$CLOUD_PASSWORD"
Pass4Aa$ditya
single quotes or ...
21
votes
Ansible will not accept the quoting that it is telling me to add
Technically speaking, none of this has anything to do with Ansible or with variables. It is basic YAML syntax.
In YAML, quotes around a string are optional as long as the string cannot be confused ...
19
votes
awk print apostrophe/single quote
In awk code, just put the apostrophe inside double quotes. You can concatenate strings by putting them next to each other (juxtaposition), no matter how these strings are constructed (literal, ...
19
votes
Why does quoting not protect filenames that start with "-" against mis-interpretation?
The reason is that while single quotes actually remove the meaning of special characters, this refers to variable expansion, globbing and word splitting - i.e. those characters that are special to the ...
18
votes
How to create alias with a command contains ' and "
Saying that the syntax of an alias is alias aliasname='command' is a bit misleading, as it seems to imply that the single quotes are part of the syntax. They are not. The part after the equal sign is ...
17
votes
Accepted
How to use jq to return information to the shell, taking whitespace into account?
The first two attempts at the end of the question do not work because they rely on the shell splitting the output of jq on whitespace. Since newline is one type of whitespace, you lose the newlines (...
16
votes
Why do options in a quoted variable fail, but work when unquoted?
You're trying to store a list of strings in a string variable. It doesn't fit. No matter how you access the variable, something is broken.
wget_options='--mirror --no-host-directories' sets the ...
16
votes
Accepted
ls with a wildcard
ls /dev/sd$device* # or
ls "/dev/sd$device"*
You must not quote the globbing metacharacters if you want globbing to be performed.
16
votes
Accepted
Why can I not access a file named `-` even when quoting the filename?
The shell removes any quotes before cat sees them. So cat - and cat "-" and cat '-' all get passed through as the array ['cat', '-'] after whitespace tokenization, wildcard expansion, and quote ...
16
votes
Why use double quotes in a [[ ]] test?
Even though the double quotes aren't necessary, reasons to use them are:
Good practice/habit: In this case they aren't necessary, but in general double quotes are to avoid unintended word splitting.
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
quoting × 1086bash × 509
shell × 500
shell-script × 231
sed × 84
ssh × 76
variable × 66
wildcards × 65
grep × 59
find × 57
command-line × 54
regular-expression × 52
filenames × 50
command-substitution × 49
escape-characters × 47
zsh × 46
awk × 41
alias × 35
arguments × 32
echo × 26
variable-substitution × 24
scripting × 21
special-characters × 21
ksh × 19
string × 19