Using the set command to redefine shell variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using the set command to redefine shell variables
# 1  
Old 01-10-2015
Using the set command to redefine shell variables

so i'm havin a bit of a issue getting set to recognize a value with spaces as a variable.


this is what i'm doing:

Code:
VAR="1 2 4 11 'dogs mouse cars' 50 19 'noise toys' "

set -- ${VAR}

When i issue a command such as the one below:
Code:
echo $5  or  echo "${5}"

i get

Code:
dog

when i should really be getting:

Code:
'dogs mouse cars'

or if i issue

Code:
echo $8 or echo "${8}"

i should get:

Code:
'noise toys'

but instead, i get:

Code:
noise



OS: Linux RedHat, Ubuntu, AIX, HPUX, SunOS
# 2  
Old 01-10-2015
Can't explain why, but this works right. There may be a shopt <option> to change the variable expansion rule??
Code:
set 1 2 4 11 "dogs mouse cars" 50 19 "noise toys"

This User Gave Thanks to ongoto For This Post:
# 3  
Old 01-10-2015
The reason your code doesn't work is because quotes aren't processed when variables are expanded. With your code, the command: echo $5 should have produced 'dogs; not dog.

With shells that support arrays, you could also try:
Code:
VARS=(1 2 4 11 'dogs mouse cars' 50 19 "noise toys")
set -- "${VARS[@]}"
printf '$5 is "%s"\n$8 is "%s"\n' "$5" "$8"

which with recent versions of bash and ksh produces:
Code:
$5 is "dogs mouse cars"
$8 is "noise toys"

Note that the quotes used when setting the elements of the array are not included in the array element; they disappear when the shell performs quote processing to group the words together in the 5th and 8th elements of the array.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Set shell variable from command

shell script has a command inside back quotes in method update_TABLE I need to store the count of number of Rows updated and store it in shell script variable "num" num = 0; Update_TABLE Update_TABLE() { `echo " set verify off feedback off echo off pagesize 0 head off... (4 Replies)
Discussion started by: finder255
4 Replies

2. UNIX for Dummies Questions & Answers

Passing Shell Variables to an awk command

Hello, I have two files File1 & File2. File1 76 135 136 200 250 345 .... File2 1 24 1 35 1 36 1 72 .... I want to get all the values form File2 corresponding to the range in File 1 and feed it to a program. Is the code below right? Can I pass shell variables to awk in this... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

3. Shell Programming and Scripting

Setting Variables WITHIN For Loop in DOS Command Shell

I'm wondering if any of you could lend an assist with a small problem. First, I'm under the impression I need to use Delayed Environment Variable Expansion (DEVE), based on other things I've read across the web. Summary: trying to use command shell (cmd.exe) in XP sp3 (if that's relevant) to... (4 Replies)
Discussion started by: ProGrammar
4 Replies

4. Shell Programming and Scripting

sed command using variables in shell script

hi guys, The following command doesn't seem to work in my shell script: tag=$(sed -n '/${line}/ s/.*\.*/\1/p' myfile.txt) When i replace the ${line} with an actual value, it works fine. So, how do i use the ${line} in this sed command? Thanks in advance, Zaff (2 Replies)
Discussion started by: zaff
2 Replies

5. Shell Programming and Scripting

how to create variables in loop and assign filename after set command?

Hi, does anybody knows how to manage, that the filenames are assigned to a variable in a loop afer getting them with set command in a ksh, like: set B*.txt i=1 c=$# x=$((c+1)) echo "$x" while ] ; do _ftpfile$i="$"$i echo "$_ftpfile$i" i=$((i+1)) done The first echo returns,... (2 Replies)
Discussion started by: spidermike
2 Replies

6. Shell Programming and Scripting

Passing shell variables to a rsh command

I noticed my script is not passing the value of variable alert to the rsh command. I need some assistance, please. This is a solaris environement. Thanks! :confused: #!/bin/sh echo -n "Alert number:" read alert rsh rhost_name 'egrep $alert /opt/var/log/*.logs' (2 Replies)
Discussion started by: lopus
2 Replies

7. UNIX for Advanced & Expert Users

How to export ENV variables, which remains set for all the shell

Hi ! How to export ENV variables, which remains set for all the shell Example :- Login :myID Pwd : **** -> Here my ID .profile is executed. Let say I set MYENV variable Kisses% rlogin ABC -l XXXGroupID -> I login into a remote Solaris Server ABC password : **** -> "XXXGroupID's... (1 Reply)
Discussion started by: dashok.83
1 Replies

8. UNIX for Dummies Questions & Answers

SET Command removed the variables

Hi, Im using csh and sometimes bash. I accidentally typed SET and looks like it has resetted some enviroment variables as im not able to run autosys jobs etc .. So i wanted to know how i can put it back to normal by replacing any profile files from another user or by editing the file which... (2 Replies)
Discussion started by: spectator
2 Replies

9. UNIX for Advanced & Expert Users

Set shell variables from SQLPLUS query results

Hi All, I needed to get the result of two sqlplus queris into shell variables. After days of looking for the ultimate solution to this problem.. i found this... sqlplus -s USER/PASS@DB <<EOF | awk '{if(NR==1) printf("%s ", $1); if(NR==2) printf("%s ", $1);}' | read VAR1 VAR2 set head off... (2 Replies)
Discussion started by: pranavagarwal
2 Replies

10. Shell Programming and Scripting

problem with set command in korn shell

I have to copy an array to a temp variable and back after doing some functions. I am trying to see if it is possible to do without while loops.My closest try was set -A temp ${THE_ARRAY} # restore array after some actions set -A THE_ARRAY ${temp} The problem with above is that, the new... (1 Reply)
Discussion started by: vijay1985
1 Replies
Login or Register to Ask a Question