LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-20-2012, 11:16 AM   #1
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 12.04 / CentOS 6.5
Posts: 394

Rep: Reputation: 61
Bash - escape all special characters in variable string?


I have the following script (test.sh):

Code:
#!/bin/bash
INPUT=$1
printf "%s\n" "LINE: ${INPUT}"
If I then do: -

./test.sh *

It produces: -

Code:
LINE: test.sh
Not "LINE: *" like I want. Now, in what I want to write, $INPUT could have many special characters in it - how do I escape them all?

I've tried: -

printf "%q\n" "LINE: ${INPUT}"

and

printf "%b\n" "LINE: ${INPUT}"

None of which work. I just want it to print any special characters within the variable $INPUT as a String. Can it be done?
 
Old 01-20-2012, 12:46 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,403

Rep: Reputation: Disabled
The problem isn't the script, it's shell expansion at work. The shell expands the asterisk to all file names in the current directory before passing it to the script.

Try:

./test \*
 
Old 01-20-2012, 12:53 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Or try surrounding the input in single quotes so the shell does not interpret any special characters
 
1 members found this post helpful.
Old 01-20-2012, 01:55 PM   #4
devUnix
Member
 
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 576

Rep: Reputation: 48
Try this:

PHP Code:
[demo@devhost ~]$ read rubbish
star is also called dash _ / \ = + & ^ % $ # wow!


[demo@devhost ~]$ echo "'$rubbish'"
'* star - is also called dash _ /  = + & ^ % $ # wow!'
[demo@devhost ~]$ 

or:

[
demo@devhost ~]$ echo "$rubbish"
star is also called dash _ /  = + & ^ % $ # wow!
[demo@devhost ~]$ 

Last edited by devUnix; 01-20-2012 at 01:57 PM.
 
1 members found this post helpful.
Old 01-21-2012, 03:32 PM   #5
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 12.04 / CentOS 6.5
Posts: 394

Original Poster
Rep: Reputation: 61
Hi,

I realise that I could do

Code:
./test.sh \*
...but this only works on...well....this example. $1 could have any number of *'s in it (which is actually what I have - the above was to show the problem), so I need to be able to escape the shell expansion from within the script. I obviously can't do: -

Code:
INPUT='$1'
as $INPUT will be literally '$1' rather than '*'. I tried using sed, but I've hardly used it before and couldn't get it to escape every '*' in $1. Little help?

Thank you for the theory as to why I'm getting the contents of the directory though - I didn't know about shell expansion
 
Old 01-22-2012, 04:51 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by arashi256 View Post
... so I need to be able to escape the shell expansion from within the script.
No, you can't do it inside the script at all. The problem is that the running shell is expanding the globs before the script is even run. When you run:

Code:
./test.sh *
...the shell first parses the line, replaces the "*" with a list of files that match the glob, and then executes the script. So the command that's actually being run is the script name plus all the files in the directory as arguments. Like this:

Code:
./test.sh file1 file2 file3 file4 etc...
You'd only get a literal "*" is if the directory is empty and nothing is matched.


The only way you can pass a raw globbing value is to escape or quote the arguments that you want to pass to the shell on the command line, or to disable globbing in the parent shell.

This is true of all shell syntax. If there's anything that's considered important by the shell on the line, it will be parsed and replaced before the command is run, unless it's protected by quotes or backslash escapes.


Edit: BTW, once a value is stored in a variable or parameter, all the characters in the string are literal, and they will be treated as such by the script as long as you properly quote it. Only unquoted parameters are ever parsed for shell-reserved characters (unless you use a specific command option like printf's "%b" or echo's "-e" to force it to interpret them).

So your only real problem is with the initial setting of "$1", and how you quote it afterwards. How about showing us exactly what you need to do, rather than fooling around with an example that may not reflect your actual purpose?

Last edited by David the H.; 01-22-2012 at 05:05 AM. Reason: as stated
 
1 members found this post helpful.
Old 01-22-2012, 02:02 PM   #7
devUnix
Member
 
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 576

Rep: Reputation: 48
Hi!


I gave you the working example above but you do not seem to check it out. Here is a script, if scrip is the only way to make you understand, that does what you expect regarding Shell Expansion:

Code:
[demo@devhost Bash]$ cat rubbish.sh 
#!/bin/bash
PARA=$1
echo "You Passed: "
echo "$PARA"
exit 0

[demo@devhost Bash]$ ./rubbish.sh '! @ # * wow stars ***'
You Passed: 
! @ # * wow stars ***
You must Single-Quote 'parameters' at the Shell/Command prompt. Then it will work fine.

If you think each character/word is to be taken as a separate parameter inside your script then you can simply split the line (PARA=$1) to work with them. That is your homework to learn more.
 
1 members found this post helpful.
Old 01-23-2012, 04:39 AM   #8
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 12.04 / CentOS 6.5
Posts: 394

Original Poster
Rep: Reputation: 61
Ah, sorry - misread. Many thanks, that fixed it. No need to be quite so snide about it, tho
 
Old 01-23-2012, 10:04 AM   #9
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 12.04 / CentOS 6.5
Posts: 394

Original Poster
Rep: Reputation: 61
Okay, since I'm also doing: -

Code:
echo $LINE >> SOME_FILE.tmp # won't work
where line has asterisks in it, you can disable bash shell expansion within your script like so...

Code:
set -f
echo $LINE >> SOME_FILE.tmp # will work
set +f
...in case anyone else has a similar problem.
 
Old 01-23-2012, 10:40 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Or you could put double quotes around $LINE
 
1 members found this post helpful.
Old 01-23-2012, 10:43 AM   #11
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 12.04 / CentOS 6.5
Posts: 394

Original Poster
Rep: Reputation: 61
Quote:
Originally Posted by catkin View Post
Or you could put double quotes around $LINE
Yeah, I suppose that would be better
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] replace new line with <br /> & escape special characters ted_chou12 Linux - Newbie 5 02-07-2011 08:24 PM
How do I replace special characters in a string within a bash script? rhaup0317 Linux - Newbie 2 06-03-2008 12:56 PM
bash command for removing special characters from string kkpal Linux - Newbie 5 05-26-2008 09:14 AM
shell script question: automatic \ of escape characters in string lagu2653 Programming 2 03-08-2006 08:58 AM
Handle escape characters in a string Helene Programming 7 05-02-2004 12:43 AM


All times are GMT -5. The time now is 06:05 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration