csh and variable values with spaces

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting csh and variable values with spaces
# 1  
Old 06-21-2011
csh and variable values with spaces

my working shell is csh and even though if I try to run my script in plain sh, it behaves the same way. Here's a simple script:

desc='"test my changes"'
cmd="echo \"$desc\""

I want $desc to be passed as an argument to another command, but csh apparently doesn't like spaces in the string and interprets the value as this:

sh -x script.sh
+ desc='"test my changes"'
+ cmd='echo "test my changes"'
+ echo '"test' my 'changes"'
"test my changes"
This is the real problem as the subsequent command (in reality it's another script instead of an echo doesn't understand the value of $desc when it sees

echo '"test' my 'changes"'
Any solutions how to properly escape spaces in a string variable?
# 2  
Old 06-21-2011
What's your ultimate goal? I'm not quite understanding your need to jam everything into a string with extra quotation marks instead of just passing the strings you want in the first place... Instead of

cmd="command \"$stuff\""

couldn't you just run
command "$stuff"

in the first place?

Or, what exactly are you trying to pass, into what? Don't just show me code that doesn't work, I can't guess what you actually do want from it.
# 3  
Old 06-21-2011
my goal is to call another script within my script which will look like
COMMENT="To test my script"
CMD="./anotherscript.sh -c \"$COMMENT\""

I need my $COMMENT variable to expand properly as a whole stirng.
# 4  
Old 06-21-2011
That still doesn't explain the purpose behind this odd goal, which is what I was wondering, because there may be more straightforward ways to do it than the design you've chosen.

In sh, you can try eval "$cmd" to force it to re-evaluate the line again after the first expansion, which will indeed interpret the literal quotes as a string parameter.

This is dangerous however, because eval accepts all valid shell syntax, including backticks and variables. Someone could feed it a tailored string to extract variables from your program or run whatever commands they wanted.

Because of this you may wish to make the string into

CMD="./anotherscript.sh -c \"\$COMMENT\""

to prevent $COMMENT from being expanded. eval will expand it for you, but won't interpret its contents as anything other than string. I don't think this is possible in C shell, only sh.

Last edited by Corona688; 06-21-2011 at 02:36 PM..
# 5  
Old 06-21-2011
Sorry for not being clear. I'm trying to create a wrapper script that will call either script1 or script2 depending on what option users select on command line when calling my script. One more input that my script takes and later passes to script2 is the comment line which can be a string with spaces. I'm then trying to pass that string as an argument to script2. The string has to be enclosed within double quotes for script2 to accept it properly.
# 6  
Old 06-21-2011
I still don't see any need to put it all in one string like that, then. Just keep them separate, and run them like normal. You can do the double-quoting yourself instead of torturing the syntax.
case "$1" in
        exec ./myscripta -c "$2"
        exec ./myscriptb -c "$2"
        echo "Unknown command '$1'" >&2
        exit 1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to get value from set of values in csh

Hello. In csh if I declared a variable to be a set of arguments can I retrieve a particular element from that set. My code set files=(`ls`) and I want to get only one file from $files. How can I do that????(It is just an abstract example):wall: Thanks in advance :) (5 Replies)
Discussion started by: FUTURE_EINSTEIN
5 Replies

2. Shell Programming and Scripting

Variable with several values and spaces.

Hello all. I am a newb obviously and a bit stumped on this, so any help gratefully accepted. The script is extracting metadata from individual mp3 files, then (hopefully will be) sorting them into newly-created subdirectories. I have filtered out the relevant metadata and have the album names... (8 Replies)
Discussion started by: spoovy
8 Replies

3. UNIX for Dummies Questions & Answers

Get all values separated with spaces(solved)

Hi, i have this text: X (m) 4917536.9627 4917536.9673 0.0090 -0.0046 Y (m) -815726.1383 -815726.1294 0.0061 -0.0089 Z (m) 3965857.4730 3965857.4840 0.0071 -0.0110 X (m) 4917536.9627 4917537.1411 -0.1784 0.1710 Y (m) -815726.1383 -815726.4859 0.3476 0.3489 Z (m) 3965857.4730... (2 Replies)
Discussion started by: limadario
2 Replies

4. Shell Programming and Scripting

Csh Problem using spaces in string variables

I have done something like this set phases = "a b" set phases = "phases="$phases echo $phases I get phases=a instead of phases=a b (3 Replies)
Discussion started by: kristinu
3 Replies

5. Shell Programming and Scripting

Storing values in arrays using csh

I am reading a number of files but then I want to put the ranges xmin xmax ymin ymax as arrays for each file. Any idea how I can do this??? set j = 1 echo "Welcome $i times" while ( $j <= $i ) echo "$j" set fname = $fin-bst-misf.xy echo " "$fname ... (0 Replies)
Discussion started by: kristinu
0 Replies

6. Shell Programming and Scripting

Reading a variable in csh

I have a simple script that sets a value and reads the value in csh: set -x set a = 10 echo $a The output of the script does not show the value of a + set a = 10 + echo any help would be great. (4 Replies)
Discussion started by: pt14
4 Replies

7. Shell Programming and Scripting

help with multiline variable in csh

My shell is csh and it is required. I have a file like sample.txt ------------------------ a b c d e f g h i ------------------------ I want set the file to a variable and print it out in the same format. I have tried something like this, but not succed. % cat ~/tmp/sample.txt a b c d... (8 Replies)
Discussion started by: anykao
8 Replies

8. Shell Programming and Scripting

csh script for deleting extra spaces in text file

I am new to scripting and I needed to know if there would be an easy way to delete extra spaces in a text file. I have a file with three rows with 22 numbers each, but there is extra spaces between the numbers when it gets output by this program AFNI that I am using. What script would help delete... (2 Replies)
Discussion started by: hertingm
2 Replies

9. Shell Programming and Scripting

how can i pass parameter with spaces to csh script

Hello all i need to pass to my shell script parameter that looks like "2 3 3" inside the script i need to use this string that looks like this "2 3 3" but when i try to print the script im getting syntax error , this is my script : set s = $1 echo $s (1 Reply)
Discussion started by: umen
1 Replies

10. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies
Login or Register to Ask a Question