Strings are typically delimited by quotes, which raises the problem of dealing with strings that include quotes.
12
votes
4answers
4k 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
103 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
357 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
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 ...
2
votes
2answers
761 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
167 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/'" ...
2
votes
2answers
473 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 <?
5
votes
3answers
841 views
How to wget Youtube horrible URL
I'm going to wget youtube search page where the URL is horrible. This is example when searching searchkeyword sorted by upload date:
wget ...
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
2answers
273 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
702 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
273 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
1k 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
296 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
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} ...
1
vote
2answers
460 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 ...
11
votes
2answers
486 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
282 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 "" ...
6
votes
2answers
2k 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. ...
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?
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 ...
26
votes
7answers
3k 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
2k 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 ...