Hot answers tagged

263 votes
Accepted

Security implications of forgetting to quote a variable in bash/POSIX shells

Preamble First, I'd say it's not the right way to address the problem. It's a bit like saying "you should not murder people because otherwise you'll go to jail". Similarly, you don't quote ...
user avatar
198 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 ...
user avatar
  • 120k
149 votes
Accepted

Why is 'ls' suddenly wrapping items with spaces in single quotes?

Preface: While it may be quite satisfying to upvote an answer such as this and call it a day, please be assured that the GNU coreutils maintainers do not care about SO answer votes, & that if you ...
user avatar
133 votes

Why is 'ls' suddenly wrapping items with spaces in single quotes?

You can chose quoting style: ls --quoting-style=literal The same as: ls -N or: QUOTING_STYLE=literal ls Make it an alias, or set export QUOTING_STYLE=literal in your .bashrc to achieve pre-8.25 ...
user avatar
  • 144k
132 votes
Accepted

Slash and backslash in sed

Use single quotes for the expression you used: sed 's/\//\\\//g' In double quotes, \ has a special meaning, so you have to backslash it: sed "s/\//\\\\\//g" But it's cleaner to change the ...
user avatar
  • 43.1k
99 votes
Accepted

How can I echo dollar signs?

You just need to escape the dollar $.: echo \$PATH $PATH Or surround it in single quotes: echo '$PATH' $PATH This will ensure the word is not interpreted by the shell.
user avatar
  • 4,047
93 votes

What does ` (backquote/backtick) mean in commands?

I would like to add few more points here. The backtick `…` is actually called command substitution. The purpose of command substitution is to evaluate the command which is placed inside the backtick ...
user avatar
  • 3,051
91 votes

Parenthesis in expr arithmetic: 3 * (2 + 1)

You can use the arithmetic expansion instead. echo "$(( 3 * ( 2 + 1 ) ))" 9 In my personal opinion, this looks a bit nicer than using expr. From man bash Arithmetic Expansion Arithmetic ...
user avatar
  • 11.4k
81 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 ...
user avatar
76 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 ...
user avatar
69 votes
Accepted

How to echo `single quote` when using single quote to wrap special characters in shell?

The tutorial is wrong. POSIX says: A single-quote cannot occur within single-quotes. Here's some alternatives: echo $'It\'s Shell Programming' # ksh, bash, and zsh only, does not expand ...
user avatar
  • 54.1k
59 votes
Accepted

Checking if an input number is an integer

Remove quotes if ! [[ "$scale" =~ ^[0-9]+$ ]] then echo "Sorry integers only" fi
user avatar
  • 43.7k
58 votes
Accepted

Add arguments to 'bash -c'

You're interpreting the man page wrong. Firstly, the part about -- signalling the end of options is irrelevant to what you're trying to do. The -c overrides the rest of the command line from that ...
user avatar
  • 7,583
54 votes
Accepted

How can I delete a file named ">"?

Any of these should work: sudo rm \> sudo rm '>' sudo rm ">" sudo find . -name '>' -delete sudo find . -name '>' -exec rm {} + Note that the last two commands, those using find, will ...
user avatar
  • 219k
54 votes

Why is 'ls' suddenly wrapping items with spaces in single quotes?

A few points about the change. It was introduced in coreutils v8.25, and alignment improved in v8.26 It only happens when outputting to terminals so doesn't break scripts It disambiguates the output ...
user avatar
51 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 ...
user avatar
  • 120k
49 votes
Accepted

find and replace with sed with slash in find and replace string

Not sure if you know, but sed has a great feature where you do not need to use a / as the separator. So, your example could be written as: sed -i 's#/var/www#/home/lokesh/www#g' lks.php It does not ...
user avatar
  • 3,164
48 votes
Accepted

Parenthesis in expr arithmetic: 3 * (2 + 1)

Another way to use let bash builtin: $ let a="3 * (2 + 1)" $ printf '%s\n' "$a" 9 Note As @Stéphane Chazelas pointed out, in bash you should use ((...)) to do arithmetic over expr or let for ...
user avatar
  • 144k
44 votes

Parenthesis in expr arithmetic: 3 * (2 + 1)

There's no reason to be using expr for arithmetic in modern shells. POSIX defines the $((...)) expansion operator. So you can use that in all POSIX compliant shells (the sh of all modern Unix-likes, ...
user avatar
44 votes

Security implications of forgetting to quote a variable in bash/POSIX shells

[Inspired by this answer by cas.]But what if …? But what if my script sets a variable to a known value before using it?  In particular, what if it sets a variable to one of two or more possible values ...
user avatar
44 votes

Is there any way to print value inside variable inside single quote?

You can't expand variables in single quotes. You can end single quotes and start double quotes, though: echo 'visit:"'"$site"'"' Or, you can backslash double quotes inside of double quotes: echo "...
user avatar
  • 43.1k
44 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 ...
user avatar
43 votes
Accepted

Why does this ffmpeg command work in bash and not zsh?

In string: rtsp://user:pass@my.webserver.org:5554/my-media/media.amp?videocodec=h264 you have ? in that string, so the shell will perform pathname expansion on that string, using pattern matching ...
user avatar
  • 144k
39 votes
Accepted

using sed with ampersand (&)

& is special in the replacement text: it means “the whole part of the input that was matched by the pattern”, so what you're doing here replaces user=&uidX with user=user=&uidXsysuserid.. ...
user avatar
38 votes
Accepted

Bash script error with strings with paths that have spaces and wildcards

Inside quotes, the * will not expand to a list of files. To use such a wildcard successfully, it must be outside of quotes. Even if the wildcard did expand, the expression "${FILES}" would result in ...
user avatar
  • 70k
37 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 {} rm "{}"
user avatar
  • 479
37 votes

nested double quotes in assignment with command substitution

Once one is inside $(...), quoting starts all over from scratch. In other words, "..." and $(...) can nest within each other. Command substitution, $(...), can contain one or more complete ...
user avatar
  • 70k
36 votes

How to escape quotes in shell?

Simple example of escaping quotes in shell: $ echo 'abc'\''abc' abc'abc $ echo "abc"\""abc" abc"abc It's done by finishing already opened one ('), placing escaped one (\'), then opening another one (...
user avatar
  • 18.6k
36 votes
Accepted

What characters need to be escaped in files without quotes?

The simple solution is to put a single quote (') at the beginning and another single quote at the end, and to replace every ' character inside the file name by the 4-character sequence '\''. All ...
user avatar
35 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 -...
user avatar
  • 219k

Only top scored, non community-wiki answers of a minimum length are eligible