Some Bash questions for a newbie

Solution 1:

  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.

Solution 2:

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.

Solution 3:

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