Posts

Showing posts from November, 2013

Linux - Back To Basic Part 4 - Grouping Commands (...) and {...}

Discovered more things when reading on Bash reference manual

This time round, it is about Grouping Commands

There are 2 grouping command and they are (...) and {...}. Now, don't get confused with ${...} and $(...), they are parameter substitution and command substitution respectively.

For the following commands

(ls -ls; echo "hello world")

{ ls -ls; echo "hello world"; }

Both of them group the commands and execute as a unit.

But the differences is a following

(ls -ls; echo "hello world") will create a subshell and execute each command in the subshell. Since (...) execute commands in a subshell, any environment changes, such as setting environment variable, will not be retained and passed back to the parent shell

{ ls -ls; echo "hello world"; } will execute each command in the current shell. Thus, any environment changes will be retained. Please note that when using { and } are reserved word and a meta-characters such as meta-characters (`&l…

Linux - Back To Basic Part 3 - Shell Command Substitution $(...)

This post, it will talk more about Command Substitution.

Command substitution happen during step 6 of command line processing. It will perform command substitution on any expression in the form of $(....)

$(COMMAND) is telling the shell to execute COMMAND and substitute it with the result.

Some examples,

1. $(echo ls)

a. It will first execute echo ls that is within $(...) and the echo output is ls
b. Next, it will replace $(echo ls) with ls. So, the final command is simply ls
c. The shell will execute ls as the final command

2. ls -al $(whereis ls)

a. It will first execute whereis ls within $(...) and echo the output such as ls: /bin/ls.exe /usr/bin/ls.exe /usr/share/man/man1/ls.1.gz
b. Next, it will replace $(whereis ls) with ls: /bin/ls.exe /usr/bin/ls.exe /usr/share/man/man1/ls.1.gz and the final command is ls -al ls: /bin/ls.exe /usr/bin/ls.exe /usr/share/man/man1/ls.1.gz
c. The shell will execute the final command and ls all given path in detail

Linux - Back To Basic Part 2 - Shell Parameter Substitution $ and ${...}

Back to basic..

In my last post regarding Command Line Processing, it talks about how shell handle command line.

This post, it will talk more about Parameter Variable Substitution.

Parameter variable substitution happens at step 5 during command line processing. In general, it substitutes any expression that start with $ sign or ${...}

Let's assume that we had declare a variable ABC with

export ABC=foo

The most common form of parameter variable substitution is

echo $ABC

So, during command line processing, $ABC will be substitute with foo and the actual command is echo foo
As such, the result is

foo

What about ${...}? The shot answer is $PARAM = ${PARAM}

So, for our example, echo $ABC has the same result as ${ABC}. That is, it will echo foo

Actually, that all and as simple as this. The remaining part are common manipulating variable with ${...} syntax and the discussion are based on the scenario given above

1. ${PARAM}

As discussed, ${ABC} = $ABC and will return foo as result.

2. …