Questions tagged [quoting]
Strings are typically delimited by quotes, which raises the problem of dealing with strings that include quotes.
1,088 questions
812
votes
13
answers
1.8m
views
How do I grep for multiple patterns with pattern having a pipe character?
I want to find all lines in several files that match one of two patterns. I tried to find the patterns I'm looking for by typing
grep (foo|bar) *.txt
but the shell interprets the | as a pipe and ...
519
votes
10
answers
230k
views
How do I delete a file whose name begins with "-" (hyphen a.k.a. dash or minus)?
How do you remove a file whose filename begins with a dash (hyphen or minus) -? I'm ssh'd into a remote OSX server and I have this file in my directory:
tohru:~ $ ls -l
total 8
-rw-r--r-- 1 me ...
432
votes
4
answers
827k
views
What characters do I need to escape when using sed in a sh script?
Take the following script:
#!/bin/sh
sed 's/(127\.0\.1\.1)\s/\1/' [some file]
If I try to run this in sh (dash here), it'll fail because of the parentheses, which need to be escaped. But I don't need ...
378
votes
6
answers
422k
views
Why does my shell script choke on whitespace or other special characters?
�� or an introductory guide to robust filename handling and other string passing in shell scripts.
I wrote a shell script which works well most of the time. But it chokes on some inputs (e.g. on some ...
283
votes
3
answers
58k
views
Security implications of forgetting to quote a variable in bash/POSIX shells
If you've been following unix.stackexchange.com for a while, you
should hopefully know by now that leaving a variable
unquoted in list context (as in echo $var) in Bourne/POSIX
shells (zsh being the ...
266
votes
6
answers
501k
views
What does ` (backquote/backtick) mean in commands?
I came across the following command:
sudo chown `id -u` /somedir
and I wonder: what is the meaning of the ` symbol. I noticed for instance that while the command above works well, the one below does ...
250
votes
3
answers
62k
views
Why is 'ls' suddenly wrapping items with spaces in single quotes?
I just noticed that on one of my machines (running Debian Sid) whenever I type ls any file name with spaces has single quotes surrounding it.
I immediately checked my aliases, only to find them intact....
243
votes
7
answers
800k
views
Using sed to find and replace complex string (preferrably with regex)
I have a file with the following contents:
<username><![CDATA[name]]></username>
<password><![CDATA[password]]></password>
<dbname><![CDATA[name]]></...
223
votes
3
answers
111k
views
$VAR vs ${VAR} and to quote or not to quote
I can write
VAR=$VAR1
VAR=${VAR1}
VAR="$VAR1"
VAR="${VAR1}"
the end result to me all seems about the same. Why should I write one or the other? are any of these not portable/POSIX?
221
votes
6
answers
207k
views
How can I execute `date` inside of a crontab job?
I want to create a log file for a cron script that has the current hour in the log file name. This is the command I tried to use:
0 * * * * echo hello >> ~/cron-logs/hourly/test`date "+%d"`.log
...
214
votes
3
answers
206k
views
Quoting within $(command substitution) in Bash
In my Bash environment I use variables containing spaces, and I use these variables within command substitution.
What is the correct way to quote my variables? And how should I do it if these are ...
213
votes
6
answers
345k
views
How can we run a command stored in a variable?
$ ls -l /tmp/test/my\ dir/
total 0
I was wondering why the following ways to run the above command fail or succeed?
$ abc='ls -l "/tmp/test/my dir"'
$ $abc
ls: cannot access '"/tmp/test/my': No such ...
190
votes
7
answers
189k
views
Can't use exclamation mark (!) in bash?
I'm trying to use the curl command to access a http url with a exclamation mark (!) in its path. e.g:
curl -v "http://example.org/!287s87asdjh2/somepath/someresource"
the console replies with bash: .....
183
votes
1
answer
82k
views
When is double-quoting necessary?
The old advice used to be to double-quote any expression involving a $VARIABLE, at least if one wanted it to be interpreted by the shell as one single item, otherwise, any spaces in the content of $...
163
votes
5
answers
495k
views
How to escape quotes in the bash shell?
I'm having trouble with escaping characters in bash. I'd like to escape single and double quotes while running a command under a different user. For the purposes of this question let's say I want to ...