How do I know if I'm running a nested shell?
The $SHLVL
variable tracks your shell nesting level:
$ echo $SHLVL
1
$ bash
$ echo $SHLVL
2
$ exit
$ echo $SHLVL
1
As an alternative to spawning sub-shells you could push and pop directories from the stack and stay in the same shell:
[root@localhost /old/dir]# pushd /new/dir
/new/dir /old/dir
[root@localhost /new/dir]# popd
/old/dir
[root@localhost /old/dir]#
Can I execute nested or chained commands in UNIX shell?
What exactly are you trying to do? It's not clear from the commands you are executing. Perhaps if you describe what you're looking for we can point you in the right direction. If you want to execute a command over a range of file (or directory) names returned by the "find" command, Colin is correct, you need to look at the "-exec" option of "find". If you're looking to execute a command over a bunch of arguments listed in a file or coming from stdin, you need to check out the "xargs" commands. If you want to put the output of a single command on to the command line of another command, then using "$(command)" (or 'command' [replace the ' with a backquote]) will do the job. There's a lot of ways to do this, but without knowing what it is you're trying it's hard to be more helpful.
nested shell variables without using eval
You can use associative arrays, joining the value of both variables. For example:
declare -A databases
# initialization
databases["es:sales"]="blahblah/es/sales.csv"
databases["en:support"]="yadayada/en/support.csv"
Then, you can get the database just by:
echo ${databases["${country}:${action}"]}
This has the advantage of having the database names collected by only one variable.
Shell programming: nested expressions with command substitution
If your shell is POSIX (most are these days, except Solaris' /bin/sh
), you can nest what is called command substitution with $()
, eg.
number=$(expr substr $number 3 $(expr length $number))
Command substitution with nested backticks requires ugly layers of backslash escaping.
Writing a Nested shell script- Unable to pass argument
Your "outer" shell script is interpreting $i as if it were one of its own variables, which isn't set, thus it evaluates to nothing. Try escaping the $ so the outer shell doesn't expand it:
echo \$i
relative paths in nested shell scripts - I can only edit the calling script
You are doing this:
. /absolute/path/to/script.sh
what about doing this:
(cd /absolute/path/to;. script.sh)
It's not strictly equivalent since it creates a sub-shell, and all the variables created/modified in script.sh
will be lost on exit, but otherwise does what you want.
to store the result of the command in a variable, just do:
result=$(cd /absolute/path/to;. script.sh)
EDIT: problem with the source/dot approach is that if the called script exits, it quits the current script. To avoid that (and if you're not looking for side effects, just replace by:
(cd /absolute/path/to;script.sh)
or
(cd /absolute/path/to;bash script.sh)
if script.sh
doesn't have the proper bash shebang header.
Related Topics
Bash Sort - How to Sort Using Timestamp
How to Connect a Shell to a Pseudo Tty
Crontab Is Not Working on Amazon Ec2 Server
Cuda Compiler Not Working with Gcc 4.5 +
Writing a Program for Hiding Processes from Ps Command Result
How to Copy Folder with Files to Another Folder in Unix/Linux
How to Remove Specific Rules from Iptables
How to Get Out of 'Screen' Without Typing 'Exit'
Find the Files Existing in One Directory But Not in the Other
Stty Serial Port Settings for Parity Not Persistent
Extending a Script to Loop Over Multiple Files and Generate Output Names
Ffmpeg Fix Watermark Size or Percentage
Backing Up (And Restoring) a Plone Instance
Unable to Pass Wget a Variable with Quotes Inside the Variable
Eclipse Cdt Indexer Lost After System Update
Why Does '/Proc/Meminfo' Show 32Gb When Aws Instance Has Only 16Gb
How to Execute an Local Script in Remote Server with Parameters