bash - add backslash in front of variables w/ spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash - add backslash in front of variables w/ spaces
# 1  
Old 11-04-2008
bash - add backslash in front of variables w/ spaces

Hello, Im writing a script that works by recursively going into directories with find. But I have some directories that have spaces in them.. so I need to parse the variables to add a backslash before the spaces.

Im not exactly sure how how to do this in bash, and honestly I dont think I know how to do this in perl either Smilie

example of one of the variables I am working with is this:

$myvar = File 1/File 2/this is my file.txt

So I need to make my variable this --

$myvar = File\ 1/File\ 2/this\ is\ my\ file.txt

and if it matters, myvar is generated in a loop with the find command: `find File\ 1/File\ 2/ -name "*.txt" -print`

Thanks for any help.
Trey
# 2  
Old 11-04-2008
Quote:
Originally Posted by trey85stang
example of one of the variables I am working with is this:

$myvar = File 1/File 2/this is my file.txt

So I need to make my variable this --

$myvar = File\ 1/File\ 2/this\ is\ my\ file.txt
Thanks $deity this conclusion is wrong. Just enclose your variable content in double quotes to protect the content:

Code:
myvar="File 1/File 2/this is my file.txt"

and pass it this way:

Code:
subfunc "$myvar"

inside the function (or the external script, its the same for that matter) use it also protected. Example:

Code:
function subfunc
{
local myvar="$1"      # notice the double quotes

some_command "$some_path/$myvar" another_arg

return 0
}

I hope this helps.

bakunin
# 3  
Old 11-04-2008
Quote:
Originally Posted by bakunin
Thanks $deity this conclusion is wrong. Just enclose your variable content in double quotes to protect the content:

Code:
myvar="File 1/File 2/this is my file.txt"

and pass it this way:

Code:
subfunc "$myvar"

inside the function (or the external script, its the same for that matter) use it also protected. Example:

Code:
function subfunc
{
local myvar="$1"      # notice the double quotes

some_command "$some_path/$myvar" another_arg

return 0
}

I hope this helps.

bakunin

Thanks for the suggestions. I am unable to try it at this moment... since myvar is in a loop being generated by the find command. How could I go about adding the double quotes to it?
# 4  
Old 11-05-2008
Quote:
Originally Posted by trey85stang
since myvar is in a loop being generated by the find command.
That does not matter. There must be somewhere (a part of) a statement where this variable is populated. One of the following three possibilities:

Code:
var=some value
var=$(some command)
var=`some command`

The last one is simply to be changed to the second one (for the greater good of shell programming ;-)) ) and the first two can be enclosed in double quotes:

Code:
var="some value"
var="$(some command)"
var=`some command` => var="$(some command)"

Post the (relevant part of the) code if you are not sure how to do it.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add backslash and apostrophe to string in variable.

Hi All I want to add backslash and apostrophe to variable in my bash script. I have my variable: USER_LIST=USER1,USER2,USER3 and I want something like this: USER_LIST_DEL=/'USER1/',/'USER2/',/'USER3/' any ideas ?? (1 Reply)
Discussion started by: primo102
1 Replies

2. Shell Programming and Scripting

[sed]: syntax to insert N spaces in front of a string

Dear all, I would like to insert N blankspaces in front of a string using sed command To give an example (N=10), I tried that code: $ echo "abcd" | sed 's/^/ \{10,\}&/' but I failed, by obtaining that result: {10,}abcd Any help would be greatly appreciated, Thanks in advance,... (18 Replies)
Discussion started by: dae
18 Replies

3. Shell Programming and Scripting

[BASH] Getting a semi-tailing backslash when passing (escaped) variables to script

Heyas Figured me had a 'typo' in tui-conf-set, i went to fix it. Now, i also figured, it might be nice to have tui-conf-set report (to console, not only exit code) wether it could save the variable to the file or not. This said, I appended this code: (the tui-title and tui-echo lines are... (3 Replies)
Discussion started by: sea
3 Replies

4. Shell Programming and Scripting

Add text in front of variable

I am just trying to add specific text in front of a ${variant} but can not seem to get the syntax correct. I have tried sed -i '$a NM_004004.5' ${variant} and printf "NM_004004.5:%s\n" ${variant} with no luck. Thank you :). (7 Replies)
Discussion started by: cmccabe
7 Replies

5. Linux

How to add \ in front of $ in a script with vi?

Please help to provide command in vi to add \ in front of $ so it does not interpret next character. rm interfaces/DART/WEB-INF/classes/DART/util/TotalDisconnectUtil$1.class rm interfaces/DART/WEB-INF/classes/DART/util/TotalDisconnectUtil$2.class rm... (3 Replies)
Discussion started by: ywu081006
3 Replies

6. Shell Programming and Scripting

Perl: backslash in front of integer like \32768

In Perl, what does a backslash preceding an integer do like \32768 ? The $/ section of perlvar writes: local $/ = \32768; # or \"32768", or \$var_containing_32768 How is \32768 different from just 32768 without backslash? I do not understand the backslashes in \"32768" and... (1 Reply)
Discussion started by: LessNux
1 Replies

7. Shell Programming and Scripting

Adding a backslash in front of square brackets with sed

I'm trying to convert this line: to \ with sed. This is what I have so far: sed -e 's/\]*\)\]/\\\\\/' but this still gives me . Any suggestions? (15 Replies)
Discussion started by: lehaste
15 Replies

8. Shell Programming and Scripting

How to replace spaces excluding those within double quotes and after backslash?

In bash or perl, I would like to know how to substitute a null character (0x00) for every white space without changing the white spaces inside the block of double quotes and the white space immediately following a backslash. Suppose that sample.txt consists of the following line. "b 1" c\ 2 ... (2 Replies)
Discussion started by: LessNux
2 Replies

9. Shell Programming and Scripting

How to use backslash and variables in sed

I have a line that contains backslashes in which I want sed to substitute text with variables. The line; \\s008\2033330user$ I want to change this in \\s008.ourschool.com\2033330user$ I now use this script: USER=2033330user sed 's/\\'"$USER"'/.ourschool.com\\'"$USER/" This doesn't... (3 Replies)
Discussion started by: Tubbie
3 Replies

10. Shell Programming and Scripting

sh, ksh: command to remove front spaces from a string?

dear pro-coders, is there any command out there that takes out the front spaces from a string? sample strings: 4 members 5 members 3 members but it has to be like so: 4 members 5 members 3 members (3 Replies)
Discussion started by: pseudocoder
3 Replies
Login or Register to Ask a Question