Sponsored Content
Top Forums Shell Programming and Scripting bash shell: 'exec', 'eval', 'source' - looking for help to understand Post 302169844 by jim mcnamara on Friday 22nd of February 2008 02:20:38 PM
Old 02-22-2008
eval "translates" a value buried inside a variable, and then runs the command that was buried in there
Code:
for i in 1 2 3
do
   eval myvar="$i"
   echo "$myvar"
done
# this gives 
1
2
3
# why? because there is no metavalue or special meaning to 1 or 2 or 3

Code:
for i in ls df
do
    eval myvar="$i"
    echo "$myvar"
done
# here you get output from the ls command and the df command

exec starts another process - BUT - it exits the current process when you do this kind of thing

Code:
#!/bin/bash

exec echo "leaving this script forever  $0"   # Exit from script here.

# ----------------------------------
# The following line never happens

echo "This echo will never echo."

source

When you run a command in the shell - like another script or a command like ls -
the shell creates a subprocess (called child process). Any environment variable that got defined or changed down in the child is LOST FOREVER to the parent process.

However if you source a script (there are two ways) you force the script to run in the current process. That means environment variables in the script you ran are NOT LOST.

Code:
# script1
MYVAR="HI there!"
# end script1

Code:
# script2
# first, run script 1
script1
# now is the MYVAR variable set?  No -why? it got set in the child, when the child exited it GOT LOST
echo "MYVAR= $MYVAR"
# now source script1 :: putting a lonesome dot or the command  "source" 
#  in front of the script name have the same effect
. script1
#  . script1 == source script1  these two are the SAME THING
source script1
# now is the MYVAR variable set?  This time we see that MYVAR has a value
echo "MYVAR= $MYVAR"

This User Gave Thanks to jim mcnamara For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

difference between source, exec and ./script

What is the difference between sourcing a script, running it or execing it? (1 Reply)
Discussion started by: 98_1LE
1 Replies

2. Shell Programming and Scripting

exit status of eval exec

I am using ksh88 and I am trying to catch the return status of opening a file using a file descriptor and the exec and eval commands. However I am not having much success. Here is what I have: eval "exec $next_fh>$1" This opens the file if the file is $1 is valid, however I want to make... (1 Reply)
Discussion started by: robotball
1 Replies

3. Shell Programming and Scripting

Exec bash shell via PHP Site !

Hi everybody ! I writed php code so exec bash shell via php (SMS Send via bash shell) but i have problem as follow : 1. When i exec from linux mode : ./sms.sh --- output is "Messages ... OK". Then all message has been sent. 2. When i exec from PHP site --- return value is "Message ... OK" on... (1 Reply)
Discussion started by: lamthenhan
1 Replies

4. Programming

help me understand exec() family of calls...

how does exec() do it? on successful call of exec() family of functions, they never return... how to i emulate that. assume the standard example, execl("/bin/ls","ls",NULL); now this would never return. i m trying to emulate exec()'s never to return feature... #include<unistd.h>... (4 Replies)
Discussion started by: c_d
4 Replies

5. Shell Programming and Scripting

I need to understand the differences between the bash shell and the Bourne shell

I do not claim to be an expert, but I have done things with scripts that whole teams of folks have said can not be done. Of course they should have said we do not have the intestinal fortitude to git-r-done. I have been using UNIX actually HPUX since 1992. Unfortunately my old computer died and... (7 Replies)
Discussion started by: awk_sed_hello
7 Replies

6. Programming

Need help with this c++ source code! DOnt understand what some stuff mean.

Okay so I am just starting programming c++. I just started started to red "C++ for Dummies yesterday and theres a lot of things I do not understand from this book and this source code especially. I will first post the full source code and then post questions about certain thing, usually what they... (2 Replies)
Discussion started by: orszhak
2 Replies

7. Shell Programming and Scripting

Not able to understand use exec command

Hi i am in learning phase of unix. i was going through exec in a unix book. below is the command exec n>file exec n>>file however when i used the exec command like below , where ex is the file name exec 2>>exand then do ls -lrt then again when i do the ls -lrt to see the size of the file... (3 Replies)
Discussion started by: scriptor
3 Replies

8. Programming

Help me to understand strange 'typedef ... ' in some source...

Working on some source I've found some strange declaration in included header file. I am looking for someone's help to understand me that syntax's, as it is fine (it is compiled without any complain,) but for me it seems out of any sense! Acctually, it warning by CC compiler: " Warning: Implicit... (1 Reply)
Discussion started by: alex_5161
1 Replies

9. Shell Programming and Scripting

Shell Script Comment code blocks in a bash source file

Just began to learn on Shell Script. I got an exercise from my friend. I know how to make this happen in C, but I'm not familiar with Shell Script. Hope I can get some help from all of you. I want to write a bash script to comment code blocks in a bash source file. What I mean comment is '#', I... (1 Reply)
Discussion started by: HiFuture0801
1 Replies

10. Shell Programming and Scripting

How to understand special character for line reading in bash shell?

I am still learning shell scripting. Recently I see a function for read configuration. But some of special character make me confused. I checked online to find answer. It was not successful. I post the code here to consult with expert or guru to get better understanding on these special characters... (3 Replies)
Discussion started by: duke0001
3 Replies
SCRIPT(1)						    BSD General Commands Manual 						 SCRIPT(1)

NAME
script -- make typescript of terminal session SYNOPSIS
script [-a] [-k] [-q] [-t time] [file [command ...]] DESCRIPTION
The script utility makes a typescript of everything printed on your terminal. It is useful for students who need a hardcopy record of an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1). If the argument file is given, script saves all dialogue in file. If no file name is given, the typescript is saved in the file typescript. If the argument command ... is given, script will run the specified command with an optional argument vector instead of an interactive shell. Options: -a Append the output to file or typescript, retaining the prior contents. -k Log keys sent to program as well as output. -q Run in quiet mode, omit the start and stop status messages. -t time Specify time interval between flushing script output file. A value of 0 causes script to flush for every character I/O event. The default interval is 30 seconds. The script ends when the forked shell (or command) exits (a control-D to exit the Bourne shell (sh(1)), and exit, logout or control-d (if ignoreeof is not set) for the C-shell, csh(1)). Certain interactive commands, such as vi(1), create garbage in the typescript file. The script utility works best with commands that do not manipulate the screen. The results are meant to emulate a hardcopy terminal, not an addressable one. ENVIRONMENT
The following environment variable is utilized by script: SHELL If the variable SHELL exists, the shell forked by script will be that shell. If SHELL is not set, the Bourne shell is assumed. (Most shells set this variable automatically). SEE ALSO
csh(1) (for the history mechanism). HISTORY
The script command appeared in 3.0BSD. BUGS
The script utility places everything in the log file, including linefeeds and backspaces. This is not what the naive user expects. It is not possible to specify a command without also naming the script file because of argument parsing compatibility issues. When running in -k mode, echo cancelling is far from ideal. The slave terminal mode is checked for ECHO mode to check when to avoid manual echo logging. This does not work when in a raw mode where the program being run is doing manual echo. BSD
June 6, 1993 BSD
All times are GMT -4. The time now is 03:48 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy