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

learn more… | top users | synonyms

4
votes
1answer
2k views

sudo -u username -s “cmd arg” returns command not found

I used to execute the command: sudo -u elasticsearch -s "ulimit -Hn" and it was returning the value of ulimit -Hn as seen by user elasticsearch until I tried it on Ubuntu 11.10. There it returns: ...
3
votes
1answer
1k 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 ...
3
votes
2answers
2k views

Ignore escape characters when printing string in shell script

I just wrote a simple shell script to save a LaTeX expression as a PNG file. It works fine, except for the LaTeX-syntax for "next row", i.e. the double backslash \\. When for instance my input ...
4
votes
2answers
236 views

Understanding quotes in a bash conditional instruction

My friend is messing around with Bash scripts and he wanted me to take a look at them (he had another friend do it for him). One of the commands is if [ "`pidof transmmission-gtk`"=0 ]; what ...
4
votes
1answer
87 views

Quoting in a function results in error

How should I write the command variable to get the expected behavior? #!/bin/bash function f ( ) { echo "$2" } command="f --option=\"One Two Three\" --another_option=\"Four Five Six\"" $command ...
18
votes
6answers
10k views

How to add newlines into variables in bash script

When I do str="Hello World\n===========\n" I get the \n printed out too. How can I have newlines then?
2
votes
3answers
126 views

How to pass lines from a file to a bash script, so that each line remains undivided, even if there are spaces?

Given: $ cat lines.txt a/b 'c/d e/f' $ cat 1.sh #!/bin/sh ./2.sh `cat lines.txt` $ cat 2.sh #!/bin/sh echo p1=$1 echo p2=$2 echo p3=$3 $ ./1.sh p1=a/b p2='c/d p3=e/f' How do I change lines.txt ...
2
votes
3answers
435 views

variable concatenation not working?

I have a simple bash shell script which is driving me bonkers. All I want to do is run a command which returns a result which I will then use in another command. The result of the first command ...
4
votes
2answers
3k 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 ...
2
votes
2answers
1k views

Bash color output fails

Is it sed or echo problem? What I'm doing wrong? $> cat ~/bin/color_test.sh #!/bin/bash ColorOff='\e[0m' # Text Reset BWhite='\e[1;37m' # Bold White string="test TEST test" echo -e ...
2
votes
2answers
272 views

Bash Script Argument Regex Group Conflict

I have a simple bash script: input_dir="`dirname $1`/`basename $1`/" output_dir="`dirname $2`/`basename $2`/" ext=$3 ... ... ... echo -n `rename "-f" "'s/.*([0-9]{11}_[0-9]{11}).*\.(.*$)/$1.$2/'" ...
3
votes
2answers
1k views

How to escape < or > in a parameter in shell?

I'd like to use grep with a PCRE expression that contains the < character. Bash thinks I want to redirect, but I don't want to. How can I escape <?
9
votes
2answers
3k 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
2answers
393 views

Run ssh as a command of another ssh command

I want to run a program on a machine that I have ssh access to from a gateway machine that I can access with ssh too. When I want to run an mpi program on the target machine T, I access to the ...
0
votes
3answers
1k views

Why can't I run this script?

$ sh bashtrrrrrr # whats my pub. ip? bashtrrrrrr: line 19: unexpected EOF while looking for matching `'' bashtrrrrrr: line 20: szintaktikai hiba: váratlan fájl vég $ cat bashtrrrrrr #!/bin/bash ...
5
votes
2answers
313 views

Bash, curious about echo $variable

I was hacking at a small script earlier and noticed something I can't explain. Running this command (time wget --spider http://www.google.co.uk/) 2>&1 | egrep 'real|response' gives me ...
5
votes
1answer
3k views

Dollar sign interpolation inside quotes in bash

I am running this little test script I wrote : #!/bin/bash TESTCASE=$@ testHarness <<runTest $TESTCASE runTest from the command line : ./test.sh "1092$212" but the dollar sign is being ...
2
votes
2answers
345 views

bash quotes expansion puzzle

My script looks like: opts="-x ''" curl http://somepage $opts I want the string in $opts appended to the command. I use bash -x test.sh to check the expansion and see that the the single-quotes are ...
4
votes
3answers
3k 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} ...
1
vote
2answers
1k views

Creating an empty file having a variable name in a script

I need to create an empty file using a shell script. The filename will be determined by examining all the files in a directory matching a pattern, say TEST.*, and appending _END to it so that the new ...
14
votes
2answers
750 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, ...
3
votes
2answers
1k 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);}"' ...
3
votes
1answer
359 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 "" ...
9
votes
3answers
3k views

How to properly escape exclamation points in bash?

Today, I was caught redhanded while attempting to code golf a password generator for Twitter. import string as s,random;print ''.join(random.sample(s.letters+s.digits+s.punctuation,9)) 90 chars. ...
34
votes
2answers
2k 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?
21
votes
4answers
6k 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 ...
38
votes
7answers
6k 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 ...
5
votes
1answer
4k views

Remote for-loop over SSH

I have the following in a script for server in ${servers[@]}; do echo ${server} ssh user@${server} "for i in /tmp/foo* ; do echo ${i}; done" done But it doesn't work. Weird thing, I see $I amount ...