Identifying the commands creating subshells


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Identifying the commands creating subshells
# 1  
Old 01-30-2012
Identifying the commands creating subshells

Hi all,

This is the basic question.
I have read many books which advised to avoid creating sub shells.
e.g: use
Code:
wc -l<filename

rather than using
Code:
cat file|wc -l

.
So, how to identify whether a command creates subshell or not?
so,is it better to use
Code:
tail -n+1 file

in stead of using cat.
Whether tail or head creates sub shells?
Thanks
# 2  
Old 01-30-2012
Not a all processes are shells, and none of those create subshells.

The cat | wc -l creates a useless cat.

If you pipe into shell commands, that creates a subshell, i.e.

Code:
cat file | while read LINE
do
...
done

better written as
Code:
while read LINE
do
...
done < file

The general idea is to use as few pipes and use as few external programs as possible. Someone might write a slow-performing loop like this:

Code:
while read LINE
do
        TOKEN1=`echo "$LINE" | awk '{ print $1 }'`
        TOKEN2=`echo "$LINE" | awk '{ print $2 }'`
        TOKEN3=`echo "$LINE" | awk '{ print $3 }'`
        TOKEN4=`echo "$LINE" | awk '{ print $4 }'`
done < file

which creates four processes per loop just to do something read can do as a builtin:

Code:
while read TOKEN1 TOKEN2 TOKEN3 TOKEN4
do
...
done < file

In summary, pipes create extra processes or subshells, external programs create extra processes, and grouping ( ) create subshells, i.e.
( echo foo ; echo bar ) | myprogram
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-30-2012
Thanks! can you please provide some example for external program.?
is AWK and SED belong to external program?
Thanks
# 4  
Old 01-31-2012
While you continue to post without mentioning your Operating System and Shell, you will get generic answers.

From your Shell command prompt, you should be able to ask the system whether a command is a keyword, a Shell-builtin or not. Try it.

Code:
type cd
type if
type test
type awk
type sed


Btw. The subshell question is Shell-specific. You will find differences between "bash" and "ksh" (to name but a few).


Here's an illustration of a subshell done with parentheses in Posix Shell: Notice that a variable changed in the subshell does not change in the main shell.
Code:
FRED=21
echo "1 Main shell: ${FRED}"
(
echo "2 Subshell: ${FRED}"
FRED=$(( $FRED + 121 ))
echo "3 Subshell: ${FRED}"
)
echo "4 Main shell: ${FRED}"

1 Main shell: 21
2 Subshell: 21
3 Subshell: 142
4 Main shell: 21


Last edited by methyl; 01-31-2012 at 08:00 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Export variables to subshells

So i have a script that is like this: #!/bin/sh VARA="elementary 1 elementary 2 nursery A nursery B highschool AZ" echo "${ContentOfADifferentSCRIPT}" | sh i do not have control over the script that is inside the variable "${ContentOfADifferentSCRIPT}". however, i know that the... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

Errexit APPEARS set in subshells

Hi there, Everyone knows that errexit is NOT inherited by subshells. But does anyone know why errexit still appears as set with set +o? The following example highlights the fact that errexit appears set in a subshell (set +o) even though it's actually unset (it does NOT exit with false). $... (0 Replies)
Discussion started by: chebarbudo
0 Replies

3. Shell Programming and Scripting

Run Shell Commands after creating MYSQL Connection

Hi, I am trying to make a Shell Script using which I will update around 10K records in MySQL db each night. For that, I am currently doing the following command in FOR LOOP: mysql -D $MASTER_DB_NAME -h $MASTER_DB_HOST -u $MASTER_DB_USER -p$MASTER_DB_PASSWD -e "$SQL_Query" This command is... (2 Replies)
Discussion started by: rahulmittal87
2 Replies

4. UNIX for Dummies Questions & Answers

Understanding the concept behind subshells

Can someone post a "for dummies" explanation on how subshells work? Let's say I want to get a couple of pieces of info from the /etc/passwd file and set them as variables to be echo'd back after all data is gathered. something like for i in ${grep 5000 /etc/passwd | cut -d : -f 5}... (5 Replies)
Discussion started by: flyboynm
5 Replies

5. Shell Programming and Scripting

Identifying dupes within a database and creating unique sub-sets

Hello, I have a database of name variants with the following structure: variant=variant=variant The number of variants can be as many as thirty to forty. Since the database is quite large (at present around 60,000 lines) duplicate sets of variants creep in. Thus John=Johann=Jon and... (2 Replies)
Discussion started by: gimley
2 Replies

6. Homework & Coursework Questions

Creating a .profile, displaying system variables, and creating an alias

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Here is what I am supposed to do, word for word from my assignment page: 1. Create/modify and print a... (2 Replies)
Discussion started by: Jagst3r21
2 Replies

7. Ubuntu

Creating terminal commands

I've written a program in C, called count_0.1 which is essentially a word count program. I want to be able to use it as a command in the terminal (by typing in count), like when you type in ls, you don't have to go to a directory, find an executable and type in: ./ls I've tried: Adding... (1 Reply)
Discussion started by: usernamer
1 Replies

8. Shell Programming and Scripting

Question regarding shells and subshells when a script is run

I have the following script running with nohup on one of my servers: #!/bin/bash #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ #set log number #i=1 #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ #Check if log exits, if so incrememnt log number up so we don't clobber #while... (8 Replies)
Discussion started by: DeCoTwc
8 Replies

9. UNIX for Dummies Questions & Answers

creating your own commands

hi, i have to type certain commands again and again eg: ps -ef | grep xyz is there any way to replace the whole command by a short form like i just do : $ psl and it give me the same output as $ps -ef | grep xyz where can we store these commands ..... on unix system and create... (2 Replies)
Discussion started by: kukretiabhi13
2 Replies

10. Shell Programming and Scripting

Killing parent shells from subshells (KSH)

Hi all, I have a shell script which calls other shell scripts, depending on the input. Within a.sh, I have a command which calls b.sh (ie. ksh b.sh) Normally, we use the exit function to terminate a shell. However, if I choose to call exit from b.sh, I will return to the parent shell who... (4 Replies)
Discussion started by: rockysfr
4 Replies
Login or Register to Ask a Question