LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-06-2005, 06:13 AM   #1
BuckRogers01
Member
 
Registered: Mar 2005
Distribution: Gentoo
Posts: 232

Rep: Reputation: 30
escape string in bash script so it can be used in command line


Does anyone know how to escape a string with backslashes so that it can be used in a command line argument, such as a file name. The file name will contain spaces, 's and "s, Im thinking of a function like addslashes() in php

Cheers, Buck
 
Old 09-06-2005, 10:42 PM   #2
pnellesen
Member
 
Registered: Oct 2004
Location: Missouri, USA
Distribution: Slackware 12.2, Xubuntu 9.10
Posts: 371

Rep: Reputation: 31
This link might help (I know very little about the particulars of Bash scripts...)

http://www.cactus.org/~dak/shellscript.html

Note the 4th option under "Special Characters" - looks like you might use something like the following:

theFileName = $'Joe\\'s File'
 
Old 01-24-2008, 02:10 AM   #3
dave42
LQ Newbie
 
Registered: Jan 2008
Posts: 2

Rep: Reputation: 0
Quote it

You don't need to escape a string if you quote it - say you want to pass all the arguments to ls, instead of
ls $*
write
ls "$*"
 
Old 01-31-2008, 02:17 PM   #4
motin
LQ Newbie
 
Registered: Apr 2006
Posts: 14

Rep: Reputation: 0
There are some nifty tips about quoting multiple arguments with spaces in them here: http://g-scripts.sourceforge.net/faq.php
 
Old 06-05-2009, 06:25 PM   #5
dash9
LQ Newbie
 
Registered: Mar 2009
Posts: 6

Rep: Reputation: 1
Quote:
Originally Posted by BuckRogers01 View Post
Does anyone know how to escape a string with backslashes so that it can be used in a command line argument, such as a file name. The file name will contain spaces, 's and "s, Im thinking of a function like addslashes() in php

Cheers, Buck
You have to use printf:
Code:
echo $(printf '%q' $x)
 
1 members found this post helpful.
Old 06-05-2009, 07:31 PM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
In bash, escaping is done with the backslash. As said, if you use quotations properly you don't need to escape anything.

Code:
$ echo "some text" > empty\ file
$ cat empty\ file 
some text
$ cat "empty file"
some text
You might also write "empty" and then press TAB to autocomplete.

Secondly, note that quotation when there are variables involved might get a bit tricky. For example:

Code:
# This is incorrect, it assumes you are trying to declare a variable
# and then run a command called "bar"
$ VAR=foo bar
bash: bar: command not found
# This is correct, it saves the string "foo bar" inside $VAR
$ VAR="foo bar"
$ echo $VAR
foo bar
# We create two empty files called "foo" and "bar"
$ touch foo bar
# We try to run the "ls" command on a file called "foo bar", 
# which doesn't exist
$ ls "$VAR"
ls: cannot access foo bar: No such file or directory
# Without quotes $VAR is interpreted as a list with two file names
# hence "ls" will show the two files we created with "touch"
$ ls $VAR
bar  foo
# When we use single quotes, $VAR is not expanded, hence is looks for
# a file that is called textually '$VAR'
$ ls '$VAR'
ls: cannot access $VAR: No such file or directory
 
Old 09-01-2009, 06:38 AM   #7
tronics
LQ Newbie
 
Registered: Sep 2009
Posts: 3

Rep: Reputation: 0
I ran into this problem in a bash script that I wrote. I pass command line args to the script and then internally I run awk using some of the args passed to the bash script. Quoting the arg internally makes no difference in this case be cause my problem arg contains '#' I get around this by either passing \#blah or "#blah" instead of #blah to the bash script but I would like to be able to process this internally. The problem arg will not always contain a '#' so something like addSlashes as the OP was asking about would be nice. Any ideas how I can solve this issue without forcing the user to escape or quote the string arg iff it contains a '#' ?

Last edited by tronics; 09-01-2009 at 06:55 AM.
 
Old 09-01-2009, 06:45 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by tronics View Post
I ran into this problem in a bash script that I wrote. I pass command line args to the script and then internally I run awk using some of the args passed to the bash script. Quoting the arg makes no difference in this case be cause my problem arg contains '#' which in quotes or not is treated as a comment and the arg is substituted before evaluation. I get around this by passing \#blah in stead of #blah to the bash script but I would like to be able to process this internally. The problem arg will not always contain a '#' so I cannot simply prepend a '\' to the start of the string. Any ideas how I can solve this issue without forcing the user to escape the string arg iff it contains a '#' ?
Passing bash variable values to awk scripts runs into the problem of double parsing -- parsing both by bash and by awk.

It is difficult to give specific help without seeing your script but awk's option -v var_name=var_value might help.

Is your awk script contained within the bash script or is it in a separate file?
 
Old 09-02-2009, 12:07 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
Single quotes usually prevents interpolation.
YMMV
 
Old 09-02-2009, 03:47 AM   #10
dash9
LQ Newbie
 
Registered: Mar 2009
Posts: 6

Rep: Reputation: 1
Quote:
Originally Posted by tronics View Post
I ran into this problem in a bash script that I wrote. I pass command line args to the script and then internally I run awk using some of the args passed to the bash script. Quoting the arg internally makes no difference in this case be cause my problem arg contains '#' I get around this by either passing \#blah or "#blah" instead of #blah to the bash script but I would like to be able to process this internally. The problem arg will not always contain a '#' so something like addSlashes as the OP was asking about would be nice. Any ideas how I can solve this issue without forcing the user to escape or quote the string arg iff it contains a '#' ?
Try:
Code:
awk ... $(printf '%q' $1) ...

Last edited by dash9; 09-02-2009 at 03:50 AM.
 
Old 09-02-2009, 09:30 PM   #11
tronics
LQ Newbie
 
Registered: Sep 2009
Posts: 3

Rep: Reputation: 0
Thanks for the responses, however none of these suggestions seem to solve the problem.

Catkin: I am already doing the v1=value bit. This is how I get around $1 being the first field in awk and $1 also being the first arg to bash script so I have something like this

awk -F ":" '/^-/{sub(/^-/,"");print "someText", $1, var1, $6}' var1=$1 $2;

the first $1 is field 1 and the second is arg1 to bash script

Dash: if I did as you suggest then I think $1 would be field one not arg1

Chrism01: The problem is I don't want it to print $1 I want it to sub $1 for the first arg just not get hung up on the # that may or may not be in the first arg.

I may just have to deal with passing "#HJKHK" or \#KJHKJH to the bash script, which currently does what I need. I was just hoping for a method that eliminates special cases.

This script is rather large and I don't want to make a separate awk script just for this one line's sake.

Any further suggestions welcome.

Thanks

Last edited by tronics; 09-02-2009 at 09:38 PM. Reason: typo
 
Old 09-03-2009, 02:52 AM   #12
dash9
LQ Newbie
 
Registered: Mar 2009
Posts: 6

Rep: Reputation: 1
I don't understand what the problem is in your line. It works for me:
Code:
$ one="#acomment"; echo "-a:b:c" | awk -F ":" '/^-/{sub(/^-/,"");print "hi", $1, var1}' var1=$one
hi a #acomment
What does this command print for you?

Can you give a sample input where you have to escape #, and the expected output?

Last edited by dash9; 09-03-2009 at 03:03 AM.
 
Old 09-06-2009, 07:05 AM   #13
tronics
LQ Newbie
 
Registered: Sep 2009
Posts: 3

Rep: Reputation: 0
Quote:
Originally Posted by dash9 View Post
I don't understand what the problem is in your line. It works for me:
Code:
$ one="#acomment"; echo "-a:b:c" | awk -F ":" '/^-/{sub(/^-/,"");print "hi", $1, var1}' var1=$one
hi a #acomment
What does this command print for you?

Can you give a sample input where you have to escape #, and the expected output?
your test is not the same as mine. I pass the #value from the command line and I don't want to have to either escape it or quote it i.e. \#value or "#value". If you look at what you did you quoted the string.

one="#acomment" <==== see you quoted it

so this is not the same and I already stated that either quoting or escaping the # would work here

Quote:
Originally Posted by tronics View Post
I may just have to deal with passing "#HJKHK" or \#KJHKJH to the bash script, which currently does what I need. I was just hoping for a method that eliminates special cases.
and in my original post here

Quote:
Originally Posted by tronics View Post
I get around this by either passing \#blah or "#blah" instead of #blah to the bash script but I would like to be able to process this internally.
Regards,

Tronics
 
Old 09-06-2009, 07:29 AM   #14
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Usually you wouldn't be able to do so. Note that the contents of the comment is removed by your current interactive shell before it even reaches your script. So, since it happens *before* there's nothing that you can do inside the script to solve that. However, you can do something from the outside, which is to turn the interactive_commands shell option off.

As said, you need to do this from the outside, because, not only for the reason I comment above, but also because as the name of the option suggest, it only applies to interactive shells, and not to scripts (your script would ignore the option anyway).

Code:
$ echo #comment

$ shopt -u interactive_comments
$ echo #comment
#comment
$
 
Old 09-06-2009, 07:45 AM   #15
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by BuckRogers01 View Post
Does anyone know how to escape a string with backslashes so that it can be used in a command line argument, such as a file name. The file name will contain spaces, 's and "s, Im thinking of a function like addslashes() in php
I don't know PHP addslashes() but can tell you how to pass a string containing spaces, double and single quotes as a bash command line argument. Is this for use at the command prompt or in a bash script?

Anything in single quotes is passed without modification -- which addresses all your requirements except the single quotes. Bash concatenates touching strings so one solution is
Code:
command '<string containing anything except single quotes>'\''<another string containing anything except single quotes>
Here the single quote is escaped and bash concatenates the three strings into a single string before passing them to command. Alternatively ' could be double quoted
Code:
command '<string containing anything except single quotes>'"'"''<another string containing anything except single quotes>
Using double quotes, any double quote may be escaped using \
Code:
command "foo bar \"rab\" oof"
or
Code:
dqoute='"'
command "foo bar ${dquote}rab$dquote oof"
The ${} format is required to stop the variable name being seen as $dquoterab.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 06:56 AM
Bash Script, no new line for echo command jorisb Linux - General 5 11-05-2005 12:08 AM
Bash Script String Splitting MurrayL Linux - Newbie 1 09-21-2004 03:20 AM
Using escape string error in MySQL select command, any one can help me? myunicom Linux - General 2 02-16-2004 09:20 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration