All Questions
44
questions
181
votes
1
answer
78k
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 $...
73
votes
1
answer
13k
views
What is the difference between the "...", '...', $'...', and $"..." quotes in the shell?
Sometimes I see shell scripts use all of these different ways of quoting some text: "...", '...', $'...', and $"...". Why are there so many different kinds of quote being used?
Do ...
29
votes
3
answers
30k
views
Escaping quotes in zsh alias
Following on from this question about stripping newlines out of text, I want to turn this into a zsh alias as follows:
alias striplines=' awk " /^$/ {print \"\n\"; } /./ {printf( \" %s \",$0);}"'
I'...
25
votes
5
answers
9k
views
Why doesn't alias foo='echo "This is a quote: \'"' work? [duplicate]
In order to remind myself when I try to use shopt in Zsh instead of setopt, I created the following alias, testing it first at a shell prompt:
$ alias shopt='echo "You\'re looking for setopt. This is ...
18
votes
1
answer
5k
views
Why does this ffmpeg command work in bash and not zsh?
Today I got home from work (run bash on an Ubuntu box) and tried to run some code on my local arch box with my beloved zsh and the commands were failing?
The command is below with the personal info ...
17
votes
1
answer
22k
views
Wrapping a command that includes single and double quotes for another command
I recently learned about watch, but am having trouble making it work with relatively sophisticated commands.
For example, I would like to ask watch to run the following command on zsh every three ...
16
votes
3
answers
9k
views
Loop over a string in zsh and Bash
I would like to convert this Bash loop:
x="one two three"
for i in ${x}
do
echo ${i}
done
in such a way to work with both Bash and zsh
This solution works:
x=( one two three )
for i in ${x[@...
13
votes
3
answers
30k
views
Treatment of backslashes across shells
How do echo and printf treat backslashes in zsh, bash and other shells?
Under zsh I get the following behavior:
$ echo "foo\bar\baz"
foaaz
$ echo "foo\\bar\\baz"
foaaz
$ echo 'foo\...
10
votes
4
answers
4k
views
Why does cut fail with bash and not zsh?
I create a file with tab-delimited fields.
echo foo$'\t'bar$'\t'baz$'\n'foo$'\t'bar$'\t'baz > input
I have the following script named zsh.sh
#!/usr/bin/env zsh
while read line; do
<<<$...
10
votes
1
answer
17k
views
Using a variable as a case condition in zsh
My question is the zsh equivalent of the question asked here: How can I use a variable as a case condition? I would like to use a variable for the condition of a case statement in zsh. For example:
...
8
votes
2
answers
3k
views
Running commands stored in shell variables
The following works in my shell (zsh):
> FOO='ls'
> $FOO
file1 file2
but the following doesn't:
> FOO='emacs -nw'
> $FOO
zsh: command not found: emacs -nw
even though invoking emacs -...
7
votes
2
answers
1k
views
Echoing "!" inside a string does some weird things [duplicate]
If I type in this:
echo "Hello, World!"
I don't know the name of it, but it prompts me for the next line. You know the PS2 thing. Or if you type echo \ and press Enter.
Why?
Well I know ...
7
votes
1
answer
3k
views
Why does `zip` in a for loop work when the file exists, but not when it doesn't?
I have a directory that contains several sub-directories. There is a question about zipping the files that contains an answer that I ever-so-slightly modified for my needs.
for i in */; do zip "zips/...
6
votes
3
answers
7k
views
shell: Quote string with single quotes rather than backslashes
How can I quote a string with single quotes?
Eg, I can do:
$ printf "%q\n" 'two words'
two\ words
$
Is there a way to get a single- (or double-) quoted string as output, ie:
$ MAGIC 'two words'
'...
6
votes
1
answer
2k
views
printing a string's "canonical print-escaped form"
I'm trying to write a function, I'll call it escape, that will behave like this:
% IFS=$' \t\n\000'
% escape FOO $IFS
FOO=$' \t\n\000'
In other words, escape takes two arguments, and then, taking ...