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

learn more… | top users | synonyms

2
votes
2answers
210 views

Meaning of \ before environment variable

In ksh on an old Solaris box I used: export PS1="$PWD $" to set the prompt to the current directory. It works great until you cd elsewhere, then you see that it's not evaluating PWD each time. I ...
0
votes
1answer
68 views

How can I echo dollar signs?

For example, $PATH and $HOME When i type echo $PATH it returns my $PATH, but i want to echo the word $PATH and not what the actual variable stands for, echo "$PATH" doesn't work either. It might ...
5
votes
1answer
50 views

Representing/quoting NUL on the command line

Can \0 be used on the command line? Background For testing corner cases in GNU Parallel I was curious whether all characters were correctly quoted on the command line. Most of them are: perl -e ...
3
votes
1answer
165 views

How can I run a bash variable as a command exactly without additional quotation?

I'm trying to make a script which needs to save a command to be run as a string. The string in question needs to contain quotes, and when attempting to execute it bash adds additional quotation ...
4
votes
1answer
1k views

Read space as input in shell script [duplicate]

How can I give space " " as input in shell script ? Ex : echo " Enter date for grep... Ex: Oct 6 [***Double space is important for single date***] (or) Oct 12 " read mdate echo $mdate I get the ...
2
votes
3answers
91 views

How to nest quotes in bash commands

I want to test the result of this command: dmesg | grep "Firmware patch 1563" Therefore, I try to write something like this: if [ "`dmesg | grep "Firmware patch 1563`" == "" ] But it doesn't ...
2
votes
4answers
327 views

Passing wildcard * to a (bash?) script [duplicate]

I want to to remove all color codes which look like '@n', '@R' etc, from a moderately large size collection of text files. So in a file called 'remove_cc', I wrote the following: sed -ie 's/@r//g' ...
1
vote
1answer
107 views

Regex works in shell but not through Perl script

I'm having trouble executing a Perl script through the Unix shell using Perl's system command. I've had more complex regex commands I had to adjust accordingly to convert from Unix to Perl, and ...
4
votes
2answers
263 views

How to escape single quotes correctly creating an alias

I've been given this one-liner to know how much memory my processes consume. $ ps -u $USER -o pid,rss,command | \ awk '{print $0}{sum+=$2} END {print "Total", sum/1024, "MB"}' Now I'd like to ...
2
votes
2answers
51 views

Is there an improved version of “pick” that will work with filenames with spaces?

Is there a version of the pick script that will work properly with spaces in the filenames? This is a tricky question—pick is trivial to enhance to work with spaces in filenames, but the output ...
2
votes
3answers
380 views

Call sed to replace a string stored in a bash variable

I want to expand a variable in bash with sed or awk. This variable is an array. For example, the script must delete strings contained in array. I tried ARRAY1=( string1 string2 string3 ) sed -i ...
0
votes
5answers
895 views

bash event not found trying to match and exclude parenthesis in grep

In a very long line I'll summarize with: (foo),(bar,baz(word,right),(end) I want to print only: (bar,baz(word,right To match the second parenthesis, I exclude the word that follows the ...
18
votes
2answers
3k views

Quoting within $(command substitution) in Bash

In my Bash environment I use variables containing spaces, and I use these variables within command substitution. Unfortunately I cannot find the answer on SE. What is the correct way to quote my ...
3
votes
1answer
41 views

Why do I get an error when double quoting single quotes in a script that launches a remote SSH command?

I have this script: port=$1 cmd=$2 ssh root@127.0.0.1 -p $port "'"$cmd"'" When running myscript.sh 1000 "ls /dev/shm" it get this error: bash: ls /dev/shm: No such file or directory In the ...
5
votes
3answers
439 views

Bash script error with strings with paths that have spaces and wildcards

I am having trouble getting the basics of Bash scripting down. Here's what I have so far: #!/bin/bash FILES="/home/john/my directory/*.txt" for f in "${FILES}" do echo "${f}" done All I want to ...
4
votes
6answers
13k views

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

I have a file with the following contents: <username><![CDATA[name]]></username> <password><![CDATA[password]]></password> ...
2
votes
0answers
181 views

How to pass multiple commands via pssh and multiple xterms

I'm attempting to open multiple xterms and run a command as an SAP user via sudo using pssh. So far, I'm able to run PSSH to a file of servers with no check for keys, open every xterm in the servers ...
25
votes
3answers
3k views

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 ...
0
votes
0answers
55 views

What is the difference between double quotes (“”) and single quotes ('')? [duplicate]

I really do not know the usage of " and '. I want to know usage of " and '. What is the difference between echo "hi" and echo 'hi'.
1
vote
2answers
73 views

When to use bash ANSI C style escape, e.g. $'\n'

I don't use bash often but I recall in the past to pass a tab or newline on the command line I would have to escape the character using the special $ character before a single quoted string. Like ...
9
votes
2answers
4k 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 ...
0
votes
0answers
29 views

Zsh - Modify File Completion w/ Quotes

I've been using Zsh recently, and when I complete a file name within quotes, the completion adds a space after the completed file name. Interestingly, this does not occur when I complete directory ...
7
votes
2answers
541 views

Bash string concatenation used to build parameter list

Given this piece of bash: PARMS='-rvu' PARMS+=" --delete --exclude='.git'" echo $PARMS rsync ${PARMS} . ${TARGET} The echo shows the PARMS string as expected, no error is displayed, but rsync ...
4
votes
1answer
256 views

Bash script for scp interpreting tilde (~) too soon

So, I just started a new C++ class that requires me to program in a Linux environment. I don't have Linux installed on my machine, but I do have OS X and I can ssh into a linux machine in the lab at ...
1
vote
1answer
94 views

Copy newest file in directory over SSH

How might one copy the newest text file from a particular directory on a remote server to his local machine? On the server itself I can do the following: $ cat `cd /home/user/dir ; readlink -f $(ls ...
2
votes
3answers
682 views

Checking if an input number is an integer

I'm trying to check if an input is an integer and I've gone over it a hundred times but don't see the error in this. Alas it does not work, it triggers the if statement for all inputs ...
2
votes
1answer
160 views

How can I pass a filename containing percent signs (%) as a parameter to a shell script in cron? [duplicate]

We have currently setup a cron job for our new script as below: 46 22 * * * /gzm_common/diagbat9/global/prod/chgstatus.sh /backups/diagbat9/Demantra/output/SK_ITEM_EXTRACT_MFGPRO_$(date +%m%d%y).txt ...
1
vote
3answers
4k views

How can I run a “grep | grep” command as a string in a bash function?

I'm trying to build a command that pipes the results of one grep command to another grep command in a bash function. Ultimately, I want the command executed to look like this: grep -I -r FooBar ...
3
votes
2answers
129 views

Replace a backslash with sed

I've been trying to create a script that would replace every occurence of C:\ in two different types of file by /root. My arborescence looks like : - Batch - Place of the script - Conf - Appli1 ...
0
votes
1answer
43 views

Quote escaping struggle with grep and sed

I'm writing a bash script to extract data from html tags and I found this command in another thread : grep -o '<tr>.*</tr>' HTMLFILE | sed 's/\(<tr>\|<\/tr>\)//g' > NEWFILE, ...
3
votes
3answers
260 views

Preventing a specific space from being expanded in a variable

In bash how do you avoid a specific space from being expanded in a variable? Lets say I have this JAVA_OPTS="-Xmx1g" JAVA_OPTS="$JAVA_OPTS -XX:OnError='/path/to/a/script.sh %p'" function args() { ...
4
votes
2answers
73 views

Inconsistent redirection behavior ssh interactive vs commands

These work, ssh remote 'echo hi > hi.txt' echo hi | ssh remote 'cat > hi.txt' but this does not work ssh remote sh -c 'echo hi > hi.txt' I expected to produce a file on remote named ...
4
votes
1answer
1k views

command: ls /etc | sort | grep d* is yielding no results but ls /etc | sort | grep p* lists entire directory

I am playing around with piping and grep tonight. I know that grep uses regex and that * means 0 or more occurrences of the preceding character. So the way I understand it is that if I do the ...
1
vote
1answer
79 views

Handling sed variables with paths in them?

I have an applescript for doing certain things, like search and replace. set lol to quoted form of POSIX path of (choose file with prompt "VälJ Fil") That chooses a file then im doing this set ...
3
votes
5answers
757 views

How to use a shell variable inside sed's s command? [duplicate]

we know that we can get the value of a variable using $ sign: x=3 echo $x 3 Is there other ways that we can use to get the value without using $ sign. I'm asking this because $ sign is a special ...
2
votes
2answers
103 views

How to escape spaces etc in passed variable, for system call to cp in awk

As the title suggests, this problem has enough layers of complexity to make simple escaping characters difficult for me. I have a bash script which, for a large part, has an embedded awk script that ...
2
votes
2answers
490 views

Replace value within single quotes in a line that contains certain word using sed command

I have a line as below in a html file, shahar.push(['remoteUrl', 'staging.zazzercode.com']); Depending upon environment, I want to replace the remoteUrl value with sed command. eg, ...
51
votes
8answers
12k 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 ...
4
votes
2answers
2k 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);}"' ...
5
votes
1answer
677 views

What does su - `whoami` command do?

I am running a script in my project and I see this command su - `whoami` I am having a few basic questions: What does this command do ? What is the significance of `` quotes outside the whoami ...
3
votes
2answers
66 views

How to build space separated, escaped full paths of files recursively matching glob?

I have a program which requires paths to files as arguments, which cannot be quoted and must not depend on working dir. Can this be done without chaining multiple xargs, tr, find, readlinks? Example ...
1
vote
2answers
2k views

Unable to use date command to change specific date format in Bash shell on OS X terminal

I am using the date -d command to change a specific date format to another. Below is the example used currDate=`Wed 12 Feb 2014` formattedDate=`date -d"${currDate}" +%Y%m%d` echo $formattedDate
3
votes
3answers
704 views

Quote a variable in shell script “if” statement

e.g. if [ "$FOO" = "true" ]; then vs if [ $FOO = "true" ]; then What is the different? Seems both of two statements also works.
2
votes
1answer
204 views

Backslash with pipe in system command from awk

I want to invoke shell command from awk so I am using system() but when I am passing expression in I am getting error at backslash. Here is my expression echo "select * from abc limit 1;"| ...
5
votes
1answer
709 views

Why do I have to escape a “dot” twice?

I know that we can escape a special character like *(){}$ with \ so as to be considered literals. For example \* or \$ But in case of . I have to do it twice, like \\. otherwise it is considered ...
0
votes
2answers
122 views

Why does passing a HEREDOC as a string to `ruby -e` not work?

For reasons I don't understand, it seems to be impossible to pass a heredoc as an expression to a Ruby interpreter: $ ruby -e <<END heredoc> puts 'hi' heredoc> END ruby: no code ...
4
votes
2answers
528 views

Add arguments to bash -c

Let say that I want to run a command through bash like this: /bin/bash -c "ls -l" According to bash man page, I could also run it like this: # don't process arguments after this one ...
1
vote
1answer
87 views

How could I retain special character without replacing it with underscore?

I'm working on a file transfer script where I encounter a problem, when the file name look like Dr' A. The transfer script will fail due to following error: sftp> put /path/Dr'\ A.tif Unterminated ...
22
votes
7answers
17k 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?
5
votes
1answer
200 views

Test -n gives unexpected result

I understand test -n <expression> to return false if the expression evaluates to a string length of greater than 0. Why then does the following happen? Macbook:~ echo ${#undeclared_variable} 0 ...