User-defined variables are local to the shell or process in which they are set.
If you don't give export an argument, you will see the variables that are already exported.
$ exportHow many lines of output did the export command produce? __________
What command did you use to count them? _________________________
| $ echo $$ | This shows the PID of current process |
| $ x=4 | Set the valueof x to 4 |
| $ export x | Export the value of x to subsequent children |
| $ bash | Create a subshell |
| $ echo $$ | Display the PID of current process |
What is the process ID (PID) of your
subshell?__________
What command did you type to find it?
________________________
Use the echo command to find the value of x.
Change the value of x to 400 in subshell.
What command do you use to assign 400 to the variable x? ______________________________
What command do you use to check the value of your x variable? _________________________
Exit the subshell by typing either exit or <Ctrl>d.
What command do you use to check the shell's process ID?______________________________
What command do you use to check the current value of x?____________________
Did the parent process inherit the new value of x?___________________________
Why or why not?________________________________________________________________
___________________________________________________________________
Create a simple shell script which returns the date. This can be done without an editor by using the cat command and redirecting output to a file.
$ cat > datescriptInvoking Shell Scripts
date
<Ctrl>d
There are several ways to execute a script.
===> 1. Use a shell to run the command datescript
$ chmod 755 datescriptThe permission characters would look like this:
-rwxr-xr-x
/ | \
owner group otherUsers must have both read and execute permission on the script file to invoke it like this:
$ datescript
bash: datescript: command not foundWhy do you get the "command not found" error message?
__________________________________________________________________________
What is the definition of your current path?
$ echo $PATH__________________________________________________________________________
Is the current directory in your path? __________________________===> 3. Use the dot (.) or source Command
$ pwdPreceed your command with a ./ to tell bash to look in the current directory.
$ ./datescript
Tue Oct 16 17:55:23 PDT 2001
$ cat datescript
date
today="my birthday"
echo $today
What command do you use to make the
script executable?_____________________________
Execute the script the normal way and notice the output:
$ ./datescriptWhat is the value of the today variable? _____________________________________
Check the value of today in your current shell. Remember it was set in the script.
$ echo $todayDoes the current shell have a value defined for today?____________________
Why or why not? _____________________________________________________
___________________________________________________________________
$ . datescript
$ echo $today
Can you explain what has happened?_____________________________________
__________________________________________________________________
__________________________________________________________________
After a command has completed (or attempted to complete) it sends a return code (or exit code) to the parent process.
To access the return code type the
following right after sending the command for execution:
$ echo $?
Example:
$ date
Tue Oct 16 18:31:33 PDT 2001
$ echo $?
0
Run your datescript shell script and find out the return code. What is it? __________________
Change to the directory above your current directory.
$ cd ..Now attempt to execute datascript even though it doesn't exist in the current directory.
$ ./datescriptWhat is the return code?____________
What do you think the two different return codes mean?____________________________________________________________________________________________________________________