I am studying Bash, but there are some things that are not explained in my book. First I'll post a script, and then I will ask questions going through scripts.

Bash Script:

$ cat sortmerg
usage ()
if [ $# -ne 2 ]; then
echo "Usage: $0 file1 file2" 2>&1
exit 1

# Default temporary directory
: ${TEMPDIR:=/tmp}

# Check argument count
usage "$@"

# Set up temporary files for sorting

# Sort
sort $1 > $file1
sort $2 > $file2

# Open $file1 and $file2 for reading. Use file descriptors 3 and 4.
exec 3<$file1
exec 4<$file2

# Read the first line from each file to figure out how to start.
read Line1 <&3
read Line2 <&4

# Strategy: while there is still input left in both files:
# Output the line that should come first.
# Read a new line from the file that line came from.
while [ $status1 -eq 0 -a $status2 -eq 0 ]
if [[ "$Line2" > "$Line1" ]]; then
echo -e "1.\t$Line1"
read -u3 Line1
echo -e "2.\t$Line2"
read -u4 Line2

# Now one of the files is at end-of-file.
# Read from each file until the end.
# First file1:
while [ $status1 -eq 0 ]
echo -e "1.\t$Line1"
read Line1 <&3
# Next file2:
while [[ $status2 -eq 0 ]]
echo -e "2.\t$Line2"
read Line2 <&4
# Close and remove both input files
exec 3<&- 4<&-
rm -f $file1 $file2
exit 0


  1. First of all, how do I indent codes in Gedit? I copied and pasted codes from a book, and it doesn't indent the codes automatically. Do you normally use Gedit or any other editor popular for scripting bash?

  2. : ${TEMPDIR:=/tmp}
    Can you explain what this is? I have programming knowledge of C# and other programming languages. So can you tell me, assuming I'm not total newbie at programming, what the : key and the curly bracket keys do?

  3. if [[ "$Line2" > "$Line1" ]]; then
    [] is same as test. But why does [[]] do any different?

  4. status1=$?
    what is $? ?

Many thanks in advance.

3 Answers 3

  1. I use vim for writing shell scripts.

  2. ${TEMPDIR} will expand to the value of the variable named TEMPDIR. ${TEMPDIR:=/tmp} will do the same, but if it's empty (or unset), the value /tmp will be assigned to TEMPDIR, and also expanded.

    Having ${TEMPDIR:=/tmp} alone on a line, will cause it to be changed to e.g. /tmp which will try to execute /tmp as a command (which will obviously fail, since you can't execute a directory). That's why the : (null) command is used. The null command ignores all input, all arguments, and does absolutely nothing. Run help : to see the description of that builtin command.

    See http://mywiki.wooledge.org/BashFAQ/073 For the various things you can do with Parameter Expansion.

  3. [[ "$line2" > "$Line1" ]] returns true if line2 sorts after line1 (like strcmp in C).

    [("test" command) and [[ ("new test" command) are used to evaluate expressions. [[ works only in Bash, Zsh and the Korn shell, and is more powerful; [ and test are available in POSIX shells.

    See http://mywiki.wooledge.org/BashFAQ/031 for the dfference between the [ command and the [[ keyword.

  4. ? is a special parameter that holds the exit status of the last command executed. $? expands the value of that parameter.

On a side note, if that's an example from your book, I'd say that's a poor source for learning bash. I recommend reading http://mywiki.wooledge.org/BashGuide which also teaches good practices.

  • hope you dont mind with edit, just trying to improve yours.
    – rɑːdʒɑ
    Oct 10, 2013 at 15:49
  • Thanks Rajagenuplula, I didn't expect this much of detailed information, this truely helps me to start on studying. I hope I can do the same later. lol
    – user198436
    Oct 10, 2013 at 19:41

Yes you have indent in Gedit. Open gedit and at Edit-> Preferences you can have the automatic indent option. enter image description here

About the second point , its saying like assume the TEMPDIR is at /tmp location.

  1. To indent code in Gedit press the Tab key, and Shift-Tab to unindent. All the information you need and the hotkeys list is at Gnome Documentation section for gedit

  2. The : command is a built-in Bash command that does nothing except return 0 (true); but in this case serves to permit the evaluation of the parameter expansion ${VAR:=VALUE} which assigns VALUE to $VAR only if it doesn't exist. If $VAR (in your case $TEMPDIR) already has a value it is not modified. It is equivalent to the construct:

    [[ -z "$TEMPDIR" ]] && TEMPDIR=/tmp

    i.e., "if $TEMPDIR is empty, evaluate command at the right (assign /tmp to TEMPDIR)".

  3. The [[ command is a built-in Bash command (as also [ and test; they have executable equivalents too). The command [[ has several pros and cons versus [ or test.

    Pros: It is a built-in command, so is faster, and it has more functionality, like regular expression checking and parsing (remember: man bash is your friend) and doesn't need to escape some operands.

    Cons: It is not portable as it has different operators available in each of the shell interpreters where it exists.

  4. The expression $? is, as the terminology in man bash states, a special parameter that resolves or "expands" to the exit status of the last command. If the last command was successful it should return true (0 for Bash), so $? will be a numeric zero; in other cases, $? will be the number returned by the command. For example this command line will show the error value returned by ls trying to find the file a-nonexistent-file.txt:

    ls a-nonexistent-file-txt ; echo $?

Bash as a programming language has a lot of quirks and peculiarities; studying the Bash manual is the route to success.

[Sorry for the apparent duplicate answer, but the others were unespecific and don't answered all the questions as they were asked (not meaning offense; I learned a couple of unrelated things there).]

  • Note that Bash has its own [ builtin
    – wjandrea
    Apr 19, 2018 at 17:34
  • @wjandrea You are totally right. I forgot mentioning it. Thanks.
    – Fjor
    Apr 23, 2018 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.