Visit The New, Modern Unix Linux Community


Variable assignments specified with eval shell built-in


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Variable assignments specified with eval shell built-in
# 1  
Variable assignments specified with eval shell built-in

According to the POSIX specifications eval is a special shell built-in, which should imply that variable assignments specified together with it should remain in effect after the built-in completes. Thus one would expect IFS to be changed after this:
Code:
var=$'a\nb c'
$ IFS=$'\n' eval '
for i in $var; do
  echo "$i"
done'
a
b c

But it isn't:
Code:
$ set | grep ^IFS
IFS=$' \t\n'


Last edited by fpmurphy; 01-08-2011 at 11:43 AM.. Reason: fixed link
# 2  
Hi.

For this script:
Code:
#!/bin/bash --posix
#!/usr/bin/env dash
#!/usr/bin/env bash

# @(#) s1	Demonstrate IFS setting.

# Section 1, setup, pre-solution.
# Infrastructure details, environment, commands for forum posts. 
# Uncomment export command to test script as external user.
# export PATH="/usr/local/bin:/usr/bin:/bin"
set +o nounset
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
# C=$HOME/bin/context && [ -f $C ] && . $C eval
version 2>&1 >/dev/null && version =o eval
set -o nounset
pe " posix setting: \"$(set -o | grep -i posix)\""
pe

# Section 2, solution.
pl " Results:"
pe " original: IFS is $(set | grep ^IFS | od -bc)"
var=$'a\nb c'
IFS=$'\n' eval '
pe " before loop: IFS is $(set | grep ^IFS | od -bc)"
for i in $var; do
  echo "$i"
done
'
pe " after loop:  IFS is $(set | grep ^IFS | od -bc)"

exit 0

The setting stays the same:
Code:
% ./s1
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 (lenny) 
eval - is a shell builtin [bash]
 posix setting: "posix          	on"


-----
 Results:
 original: IFS is 0000000 111 106 123 075 047 040 011 012
          I   F   S   =   '      \t  \n
0000010
 before loop: IFS is 0000000 111 106 123 075 047 012
          I   F   S   =   '  \n
0000006
a
b c
 after loop:  IFS is 0000000 111 106 123 075 047 012
          I   F   S   =   '  \n
0000006

I think this is what you expected.

The dash and plain bash did not keep the before-loop setting ... cheers, drl
This User Gave Thanks to drl For This Post:
# 3  
and for ksh93
Code:
 original: IFS is 0000000  111 106 123 075 044 047 040 134 164 134 156 047 012
           I   F   S   =   $   '       \   t   \   n   '
0000015
 before loop: IFS is 0000000  111 106 123 075 044 047 134 156 047 012
           I   F   S   =   $   '   \   n   '
0000012
a
b c
 after loop:  IFS is 0000000  111 106 123 075 044 047 134 156 047 012
           I   F   S   =   $   '   \   n   '

This User Gave Thanks to fpmurphy For This Post:
# 4  
Thanks guys.. Mystery solved! I ran it through ksh and dash before but I must have been looking at the wrong thing .... It is a bash-only thing then. I never realized default bash is so much different from POSIX and that you really need to use --posix for it to be compliant (at least the way it is compiled in my distribution)

That'll be item #23 on this list...
Bash POSIX Mode - Bash Reference Manual
# 5  
Note that Bash even in POSIX mode is not fully POSIX compliant. It is mostly compliant. There are weasel words used to avoid claiming full compliance.
Quote:
will cause Bash to conform more closely to the POSIX standard
Chet R has no interest in full conformance.

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #587
Difficulty: Medium
All programming languages support recursion.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Safe way to eval variable declarations?

Is there a safe way to evaluate variable declarations within a script whether they come from a .conf file, user input, or stdin? Example .conf file: server=ftp.xxxx.com port=21 user="$USER" # Hopefully allow this type of substitution domain="$DOMAIN" server="$(malicious... (4 Replies)
Discussion started by: Michael Stora
4 Replies

2. Shell Programming and Scripting

'eval' used in variable assignment

pattern1=book { x=1 eval echo \$pattern$x } book (this is the output) But when I assign a variable to the output of the eval it doesn't work unless I prefix 2 times backslash before $ as shown below. { a=`eval echo \\$pattern$x` echo $a } book Why here twice "\" has to be... (3 Replies)
Discussion started by: ravisingh
3 Replies

3. Shell Programming and Scripting

Newline in my script-built shell variable

Hi all, I'm not exactly a shell script guru, so this is probably a very silly question and I'm not seeing the point, but you know, sometimes it happens... I have this script which adds entries to local arp cache using it to find the corresponding IP address. # export MAC=00:25:90:34:3d:f2... (16 Replies)
Discussion started by: micantox
16 Replies

4. Shell Programming and Scripting

awk built-in variable for input file

Hi guys, Does awk have a built-in variable which I can use to display the input file it's currently reading? I'm currently concatenating multiple files using awk and later on do some parsing. But for now, I want to add an extra column in the main output data file - basically putting in the... (3 Replies)
Discussion started by: Det7
3 Replies

5. Shell Programming and Scripting

assignment to variable from eval command

Hi Gurus, I am having 2 parameters as below parm1=value1 parm2=parm1 I want to evaluate parm1 value using eval echo \$$parm2 and later i want to assign this value to other variable which i will be using in if statement like : if ]; then do this....... fi could you please suggest... (5 Replies)
Discussion started by: k_vikash
5 Replies

6. Shell Programming and Scripting

eval and variable assignment

Hi, i have an issue with eval and variable assignment. 1) i have a date value in a variable and that date is part of a filename, var1=20100331 file1=${var1}-D1-0092.xml.zip file2=${var2}-D2-0092.xml.zip file3=${var3}-D3-0092.xml.zip i am passing the above variables to a script via... (11 Replies)
Discussion started by: mohanpadamata
11 Replies

7. Shell Programming and Scripting

Passing eval value to a variable

Hello, I have a script that does an scp to a server and then gets the number of process running on that server, the o/P should be stored in a variable for further processing eval `echo "ssh -q $Infa_user@$host 'csh -c $CMD '"` where CMD="ps -ef | grep -i ${INFA_REPO} | grep -v grep | wc... (2 Replies)
Discussion started by: amit1_x
2 Replies

8. UNIX for Dummies Questions & Answers

built in variable to hold the name of therunning script...

Hi, I have multiple functions that can be called by any shell script. These functions have inbuilt echo statements to logs their activity into a log file. When I run multiple shell scripts that call these functions, they all log into the same log file and I am not able to differentiate which... (2 Replies)
Discussion started by: new_learner
2 Replies

9. Shell Programming and Scripting

How to assign eval value as Variable..

Im facing problem in assigning value of eval array variable as normal variable.. x=0 eval DATA${x}="FJSVcpcu" x=`expr $x + 1` eval DATA${x}="FJSVcsr" if x=0, type -> eval echo \$DATA$x , its give me FJSVcpcu i want assign this value into an variable as variable=`eval echo... (3 Replies)
Discussion started by: neruppu
3 Replies

10. Shell Programming and Scripting

eval a variable that has a .

Hi, Is there any way that I can eval the following - eval abc.csv=def.csv I am getting the - bash: command not found error. thanks. (3 Replies)
Discussion started by: ttshell
3 Replies

Featured Tech Videos