Problem running var (with spaces) as command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem running var (with spaces) as command
# 1  
Old 04-01-2010
Problem running var (with spaces) as command

I seem to be fighting bash's shell expansions..

I set this variable:
CMD="export MVAR=\"1 2 3\""

if I try to run it, it is clear the shell is parsing along the spaces of the contents of MYVAR:

> $CMD
+ export 'MYVAR="1' 2 '3"'
+ MYVAR='"1'
-bash: export: `2': not a valid identifier
-bash: export: `3"': not a valid identifier

Is there any set of escapes / quoting that will get bash to keep the MYVAR contents together when I run it? I'd prefer not to use eval, since I'm passing my CMD to a function in another script I don't control.

Thanks!
# 2  
Old 04-01-2010
Use 'eval'

Code:
CMD="export MVAR='1 2 3'"

eval $CMD

echo $MVAR
1 2 3

# 3  
Old 04-01-2010
Quote:
Originally Posted by bbw
Is there any set of escapes / quoting that will get bash to keep the MYVAR contents together when I run it? I'd prefer not to use eval, since I'm passing my CMD to a function in another script I don't control.
No. If you double quote CMD then it will expand to one token and the shell will start looking for a command to execute whose name is the entire string, spaces and all. So, you cannot double quote $CMD when you invoke it. Without the double quotes, the results of the expansion will undergo field splitting, and quotes within the expansion are not special so they cannot be used to affect the field splitting step.

(Assuming a typical posix-like shell) Your only option is eval. Without it, what you are trying to do is impossible.
Code:
CMD='eval export MVAR="1 2 3"'

Regards,
Alister
# 4  
Old 04-01-2010
Alister, thanks for the definitive answer.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem running plsql using printf command on bash shell

I am running plsql using printf on a shell, but i am getting some strange error, can someone point what exactly am i missing, $ echo $SHELL /bin/bash $ printf " > SET serveroutput ON trimspool on feed off echo off > declare > p_val number; > d_val varchar2(10); > begin > SELECT... (1 Reply)
Discussion started by: kamauv234
1 Replies

2. AIX

I'm facing problem with rpm command, when running the command and appears this error:

exec(): 0509-036 Cannot load program /usr/opt/freeware/bin/rpm because of the following errors: 0509-022 Cannot load module /opt/freeware/lib/libintl.a(libintl.so.1). 0509-150 Dependent module /opt/freeware/lib/libiconv.a(shr4.o) could not be loaded. 0509-152 Member... (4 Replies)
Discussion started by: Ohmkar
4 Replies

3. Shell Programming and Scripting

Problem with running the "autorep" command via crontab

Hi, The user "MadeInGermany" tried to help on the below post by saying "This has been asked before; see the links below. Get your current LD_LIBRARY_PATH and redefine that in your ksh script! " Thanks for the help. but this did not help. And my post got locked. I can't reply on my previous... (5 Replies)
Discussion started by: girish1428
5 Replies

4. Ubuntu

problem with /var/mail directory

Hi, There is no "administrator" file in "/var/mail" directory........ can any one tell me what could be the reason for that?...and also how to resolve that??? Thanks, ~Kavi (4 Replies)
Discussion started by: kavi.mogu
4 Replies

5. Solaris

SVM Problem Regarding /var

Hi Guys! I am facing Problem with SVM I mention the problem below: I have Sunfire V210 machine and the partitions are as follows: Part Tag Flag Cylinders Size Blocks 0 root wm 0 - 6134 29.77GB (6135/0/0) 62429760 1 swap wu ... (1 Reply)
Discussion started by: sensation
1 Replies

6. Shell Programming and Scripting

ftp var for filename with spaces

Hello all, I am having difficulties writing an ftp script to retrieve a file via get using a variable name to pass the file name. I know the name of the file I am going to retrieve, this file name has embedded spaces and punctuation in the name itself. If I interactively use the get and I... (10 Replies)
Discussion started by: gio001
10 Replies

7. Shell Programming and Scripting

Problem regarding Sed/Echo/Var Init

Greetings, I've visited this forums for a long time and normally got an right answer but this time my problem doesn't seem to go away. What I'm trying to do is the following: VAR="\n\nline1\nline2\nline3\nline4\nline5\nline6\nline7\n\n" (The count of newlines is varying!) If I echo this i... (3 Replies)
Discussion started by: ph1l
3 Replies

8. Shell Programming and Scripting

Store command in a Var problem

Hi! I strated to script since 1 month and I don't have much expirience but this thing here is strange I have a script where I want to store a svn command in a var to check it later so it's like this #!/bin/sh list=`ssh itadmin@192.168.1.200 "ls /home/svn"` # this stores the list of... (13 Replies)
Discussion started by: ruben.rodrigues
13 Replies

9. Shell Programming and Scripting

Problem with echo for var, padded with spaces

While concatenating 2 values, one which expanded to fixed width & other not, I am not getting value expanded as fixed width. Following is script for the same : #!/bin/sh var1="abc" var2="def" var1Fxd=`echo $var1 | awk '{printf("%-6s",$0)}'` echo $var1Fxd""$var2 But, if I try - echo... (2 Replies)
Discussion started by: videsh77
2 Replies

10. UNIX for Dummies Questions & Answers

Running system reports from /var/adm ?? Please help ASAP

when users ask for certain information about their systems. from my knowledge, I know that I'm supposed to go into the /var/adm file system of that user. now, can someone tell me what information exactly is contained in the /var/adm filesystems. I mean I want to know what information it is... (1 Reply)
Discussion started by: TRUEST
1 Replies
Login or Register to Ask a Question