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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
09-06-2005, 06:13 AM
|
#1
|
Member
Registered: Mar 2005
Distribution: Gentoo
Posts: 232
Rep:
|
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
|
|
|
09-06-2005, 10:42 PM
|
#2
|
Member
Registered: Oct 2004
Location: Missouri, USA
Distribution: Slackware 12.2, Xubuntu 9.10
Posts: 371
Rep:
|
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'
|
|
|
01-24-2008, 02:10 AM
|
#3
|
LQ Newbie
Registered: Jan 2008
Posts: 2
Rep:
|
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 "$*"
|
|
|
06-05-2009, 06:25 PM
|
#5
|
LQ Newbie
Registered: Mar 2009
Posts: 6
Rep:
|
Quote:
Originally Posted by BuckRogers01
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.
|
06-05-2009, 07:31 PM
|
#6
|
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083
|
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
|
|
|
09-01-2009, 06:38 AM
|
#7
|
LQ Newbie
Registered: Sep 2009
Posts: 3
Rep:
|
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.
|
|
|
09-01-2009, 06:45 AM
|
#8
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by tronics
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?
|
|
|
09-02-2009, 12:07 AM
|
#9
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,391
|
Single quotes usually prevents interpolation.
YMMV
|
|
|
09-02-2009, 03:47 AM
|
#10
|
LQ Newbie
Registered: Mar 2009
Posts: 6
Rep:
|
Quote:
Originally Posted by tronics
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.
|
|
|
09-02-2009, 09:30 PM
|
#11
|
LQ Newbie
Registered: Sep 2009
Posts: 3
Rep:
|
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
|
|
|
09-03-2009, 02:52 AM
|
#12
|
LQ Newbie
Registered: Mar 2009
Posts: 6
Rep:
|
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.
|
|
|
09-06-2009, 07:05 AM
|
#13
|
LQ Newbie
Registered: Sep 2009
Posts: 3
Rep:
|
Quote:
Originally Posted by dash9
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
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
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
|
|
|
09-06-2009, 07:29 AM
|
#14
|
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083
|
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
$
|
|
|
09-06-2009, 07:45 AM
|
#15
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by BuckRogers01
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.
|
|
|
All times are GMT -5. The time now is 01:04 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|