Strings are typically delimited by quotes, which raises the problem of dealing with strings that include quotes.
23
votes
2answers
1k 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?
26
votes
7answers
2k 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 ...
11
votes
2answers
482 views
gnu find and masking the {} for some shells - which?
The man page for gnu find states:
-exec command ;
[...] The string `{}' is replaced by the current
file name being processed everywhere it occurs in the
arguments to the command, ...
18
votes
3answers
2k views
using single or double bracket - bash
I'm confused with using single or double bracket. look at code:
dir="/home/mazimi/VirtualBox VMs"
if [[ -d ${dir} ]]; then
echo "yep"
fi
It works perfectly although the string contains space. ...
8
votes
1answer
1k views
How can I execute `date` inside of a cron tab 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
...
8
votes
2answers
1k views
What is the significance of single and double quotes in environment variables?
I defined some environment variables in my .profile like this:
MY_HOME="/home/my_user"
but the variable does not seem to evaluate unless I strip off the quotes and re-source the file. I believe ...
2
votes
1answer
372 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
3answers
4k views
Quoting in ssh $host $FOO and ssh $host “sudo su user -c $FOO” type constructs
I often end up issuing complex commands over ssh; these commands involve piping to awk or perl one-lines, and as a result contain single quotes and $'s. I have neither been able to figure out a hard ...
2
votes
2answers
118 views
Use of quotes in GNU grep regular expressions
I could see that certain patterns in the GNU Grep can be enclosed within brackets and certain others need not be. For example, matching the beginning of a word works only if it is enclosed within ...
11
votes
3answers
936 views
What does it mean to have a $“dollarsign-prefixed string” in a script?
I just saw this in an init script:
echo $"Stopping Apache"
What is that dollar-sign for?
My research so far:
I found this in the bash manual:
extquote
If set, $'string' and ...
4
votes
3answers
2k views
Why the double quotes and backquotes in a shell script?
I'm looking at this script mysql backup script and I don't understand what is the reason for using both backticks and double quotes around command names?
379 WHICH="`which which`"
380 AWK="`${WHICH} ...
9
votes
5answers
327 views
Why do I need to quote variable for if, but not for echo?
I've read that you need double quotes for expanding variables, e.g.
if [ -n "$test" ]; then echo '$test ok'; else echo '$test null'; fi
will work as expected, while
if [ -n $test ]; then echo ...
7
votes
3answers
5k views
What does ` (backquote/backtick) mean in bash?
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 ...
5
votes
2answers
99 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 ...
4
votes
2answers
2k views
Why does the exclamation mark `!` sometimes upset bash?
I realize that ! has special significance on the commandline in the context of the commandline history, but aside from that, in a runing script the exclamation mark can sometimes cause a parsing ...
3
votes
1answer
91 views
Escaping quotes for scp
I needed to write a that behaves correctly with nasty (spaces, braces, etc..)
filenames.
scp -rv "$1" shiny:/Volumes/Seagate3To/\"$1\"
This function works, but I don't understand why the quotes ...
3
votes
1answer
280 views
Difference between ' and " on command line (bash)?
I used to use '' and "" interchangeably on the command line, but I recently noticed that '$HOME/some/dir' is not expanded, while "$HOME/some/dir" is. I searched around a little bit and found that "" ...
2
votes
3answers
34 views
Can I reload variable in a watch command?
Part of my job involves some data handling. One of the tasks is to 'flatten' a set of directories (which we'll call Dir for now), and copy them to a new location called DirFlat.
This can take a long ...
19
votes
2answers
650 views
Why escape trivial characters in shell script?
I just opened a legacy shell script (written in old ksh88 on Solaris) and found the following repeated all throughout the code:
[ -f $myfile ] && \rm -f $myfile
The escaping backslash ...
4
votes
5answers
550 views
How to enclose in quotes if both single and double quotes are already used?
I don't remember the exact commands and tricks that I use sometimes which solves much of the work, so I try to log them into a file for future reference. What I typically do is just put whole command ...
2
votes
2answers
72 views
Use a parameter in a command argument
I run a find command to search files with a name containing perl:
find /tmp -name '*perl*'
/tmp/perl.pl
/tmp/run-perl-stage.pl
But when I set the perl name in a variable PARAMETER and run the find ...
2
votes
2answers
183 views
Piping paths with different types of quotes for slash substitution
I would like to use sed to convert a path with backslashes to the same path with forward slashes:
E.g.
I would like to pipe \\path\to\file\ and obtain /path/to/file
None of the following commands ...
1
vote
4answers
120 views
Syntax error in a bash script that calls find
Where is the error in this script please:
#!/bin/bash
rep="git"
files=`find' ${rep} '-type f`
for f in ${files} do
echo $f
done
When i run find git -type f alone in the shell, it works!