Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Identifying the commands creating subshells Post 302594131 by Corona688 on Monday 30th of January 2012 02:08:30 PM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
ECHO(1) 						    BSD General Commands Manual 						   ECHO(1)

NAME
echo -- write arguments to the standard output SYNOPSIS
echo [-n] [string ...] DESCRIPTION
The echo utility writes any specified operands, separated by single blank (' ') characters and followed by a newline (' ') character, to the standard output. The following option is available: -n Do not print the trailing newline character. The end-of-options marker -- is not recognized and written literally. The newline may also be suppressed by appending 'c' to the end of the string, as is done by iBCS2 compatible systems. Note that the -n option as well as the effect of 'c' are implementation-defined in IEEE Std 1003.1-2001 (``POSIX.1'') as amended by Cor. 1-2002. For porta- bility, echo should only be used if the first argument does not start with a hyphen ('-') and does not contain any backslashes (''). If this is not sufficient, printf(1) should be used. Most shells provide a builtin echo command which tends to differ from this utility in the treatment of options and backslashes. Consult the builtin(1) manual page. EXIT STATUS
The echo utility exits 0 on success, and >0 if an error occurs. SEE ALSO
builtin(1), csh(1), printf(1), sh(1) STANDARDS
The echo utility conforms to IEEE Std 1003.1-2001 (``POSIX.1'') as amended by Cor. 1-2002. BSD
November 12, 2010 BSD
All times are GMT -4. The time now is 11:14 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy