Linux command subshell () parentheses plainly

Linux command subshell () parentheses plainly

A subshell is a collection of commands written from (to) the shell prompt.

[ec2-user@humidai ~]$ pwd
[ec2-user@humidai ~]$ (cd /home/ec2-user/;vi test.json)
[ec2-user@humidai ~]$ pwd

In the subshell, use cd /home/ec2-user/ to move to the directory, and then use the vi command to open the test.json file. Put a semicolon between the commands.

Closing the file returns you to the shell prompt, and pwd will be /.

In (), cd /home/ec2-user/, so it looks like the current directory is moved at the shell prompt, but it is not.

This is a feature of subshells: they do not affect the shell prompt.

You can loop in double parentheses and run the subshell inside the loop.

[ec2-user@humidai ~]$ for((i=1;i<=5;i++))
> do
> (cd /home/ec2-user/; mkdir "test"$i)
> done

This will create the directories test1,test2,…. . test5 directories will be created.

Shell file execution in a subshell

You can also execute shell files in a subshell.

[ec2-user@humidai ~]$(cd /home/ec2-user/; &)