Questions tagged [quoting]

Strings are typically delimited by quotes, which raises the problem of dealing with strings that include quotes.

Quoting is a common issue in (a.k.a. ). Here are some common problems:

1054 questions
797
votes
13 answers

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 complains when bar isn't an executable. How can I…
Dan
  • 9,372
  • 5
  • 25
  • 39
483
votes
10 answers

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 staff 1352 Aug 18 14:33 --help ... How in the…
Astra
  • 4,933
  • 2
  • 16
  • 6
400
votes
4 answers

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 to escape the backslashes themselves (between…
detly
  • 5,020
  • 6
  • 23
  • 29
370
votes
6 answers

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 file names). I encountered a problem such as the…
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
266
votes
4 answers

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 exception) has a very special meaning…
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
255
votes
6 answers

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 not: sudo chown 'id -u' /somedir
gws
  • 2,661
  • 3
  • 14
  • 6
243
votes
3 answers

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. wyatt@debian630:~/testdir$ ls 'test 1.txt' …
Wyatt Ward
  • 3,962
  • 2
  • 14
  • 21
219
votes
7 answers

Using sed to find and replace complex string (preferrably with regex)

I have a file with the following contents: and I need to make a script that changes the "name" in the first line to "something", the…
Harry Muscle
  • 2,437
  • 2
  • 13
  • 8
210
votes
6 answers

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 Unfortunately I get this message when that…
cwd
  • 44,479
  • 71
  • 146
  • 167
209
votes
5 answers

$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?
xenoterracide
  • 57,918
  • 74
  • 184
  • 250
199
votes
3 answers

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 nested? DIRNAME=$(dirname "$FILE") or do I quote…
CousinCocaine
  • 2,155
  • 3
  • 13
  • 12
179
votes
7 answers

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: ... event not found. What is going on here? and…
netbrain
  • 2,125
  • 2
  • 15
  • 12
175
votes
1 answer

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 $VARIABLE would throw off the shell. I understand,…
kjo
  • 14,779
  • 25
  • 69
  • 109
173
votes
6 answers

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 file or directory ls: cannot access 'dir"': No…
Tim
  • 98,580
  • 191
  • 570
  • 977
144
votes
5 answers

How to escape quotes in 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 echo the following on the screen: 'single quote…
m33lky
  • 2,505
  • 5
  • 22
  • 20
1
2 3
70 71