different result for [[ ]] and [] string comparator "<" - bash

I was doing some tests, and I couldn't understand why there two results are different. The first one seems correct, since in a crescent ordenation, the character 'f' comes first than 'F'
$ [[ "Foo" < "foo" ]]
$ echo $?
1
So why this one is incorrect?
$ [ "Foo" \< "foo" ]
$ echo $?
0

Both [[ and [ are built into bash. [ is equivalent to the test command, also a builtin. (Well, almost equivalent; [ requires a matching ], test doesn't.)
According to the bash manual:
When used with test or [, the < and > operators sort
lexicographically using ASCII ordering.
(Bourne shell builtins are documented here).
But the expression in [[ ... ]] follows the rules of Bash conditional expressions:
When used with [[, the < and > operators sort
lexicographically using the current locale. The test command uses
ASCII ordering.
(There's another test / [ command provided as part of the GNU coreutils package, typically /usr/bin/test. This command doesn't provide < and > operators, which are not specified by POSIX. The external command should be irrelevant if you're using bash, unless you explicitly give the full path /usr/bin/test.)
Your question is tagged both "bash" and "sh". If you're using bash, you should probably use the bash-specific [[ feature rather than [. If you want greater portability (e.g., if your script might be used with a shell other than bash), you can't rely on the [[ builtin, and you can't assume that [ or test supports < and >.

The BSD test implementation of <[1], like the bash-builtin one, is not character-collation-order aware; it refers only to "the binary value" of the characters in question.
The bash [[ ]] implementation of < is characterset-aware: It honors the current language/locale's selected collation order.
If you set LC_COLLATE=C (specifying ASCII sort order), then [[ ]] will do the same:
$ (export LC_COLLATE=C; [[ "Foo" < "foo" ]]; echo $?)
0
[1] - > is not a POSIX-standardized test operator, so all answers must be in the context of a specific implementation.

Related

Bash to zsh actual syntax differences, such as brackets in 'if' [duplicate]

The zsh_test.sh is simple, as following:
#!/usr/bin/env zsh
if [ $USER == 'root' ]; then
echo "root"
else
echo "not root"
fi
Copy and paste the above codes into a zsh shell, it executed well.
$ #!/usr/bin/env zsh
$ if [ $USER == 'root' ]; then
then> echo "root"
then> else
else> echo "not root"
else> fi
not root
But directly execute script file zsh_test.sh, got an error.
$ ./zsh_test.sh
./zsh_test.sh:3: = not found
I now see what's wrong: You are the victim of a fairly obscure zsh mechanism, which is described in the zshexpn man page and is called '=' expansion. From the man-page:
If a word begins with an unquoted `=' and the EQUALS option is set, the remainder of the word is taken as the name of a command. If a command exists by that name, the word is replaced by the full pathname of the command.
You can try it with the command
echo ==
which also outputs this error message. For instance, on my platofm
echo =ruby
outputs /usr/bin/ruby, because this is where I have ruby installed. If you would have in your PATH a program named =, the == would resolve to this path.
While it is unusual to use a double == sign inside [ ... ], the zsh implementation of this command allows it, but you would have to quote the operator, to avoid =-expansion:
if [ $USER '==' root ]; then
An alternative would be to use [[ ... ]] instead. This is not a command, but a syntactic construct, and expansion rules are different inside it. Therefore
if [[ $USER == root ]]; then
would work as well.
I'm afraid that you are using test command wrong. Let's see why.
test command is defined since Unix version III. You can often find this command also as [ binary in your PATH. In most modern shells (let's pretend that bash is modern shell as well), there is also implementation of test or [ as builtin command. From the specification, the only valid way to compare two strings is this:
STRING1 = STRING2
the strings are equal
STRING1 != STRING2
the strings are not equal
Original strict POSIX implementation of test command is somehow limited and can be difficult to use. But it is portable, and that it's main strength. But what if you don't care about portability at all? Then there are Conditional Expressions.
Conditional Expressions, available as [[ builtin command, are improved, not POSIX compatible replacement for original test command. Look in the manual for the things you can compare with them to get the idea. Double equality sign (==) is also supported (and the documentation explicitly says it's for compatibility with other sorts of computer language.)
Conclusion?
When you are writing scripts for particular shell, like zsh, and you are absolutely sure that portability is not important for you, always use [[ instead of [. Your life will be easier and change to your script is minimal:
#!/usr/bin/env zsh
if [[ $USER == 'root' ]]; then
echo "root"
else
echo "not root"
fi
If portability between different shells and environments is necessary, you will have to use original test or [ command, and forget about zsh, == and many other things at all.
#!/bin/sh
if [ "$USER" = 'root' ]; then
printf '%s\n' "root"
else
printf '%s\n' "not root"
fi

how to match a specific file extension in shellscript

I looked some other posts and learnt to match file extension in the following way but why my code is not working? Thanks.
1 #!/bin/sh
2
3 for i in `ls`
4 do
5 if [[ "$i" == *.txt ]]
6 then
7 echo "$i is .txt file"
8 else
9 echo "$i is NOT .txt file"
10 fi
11 done
eidt:
I realized #!/bin/sh and #!/bin/bash are different, if you are looking at this post later, remember to check which one you are using.
The [[ ]] expression is only available in some shells, like bash and zsh. Some more basic shells, like dash, do no support it. I'm guessing you're running this on a recent version of Ubuntu or Debian, where /bin/sh is actually dash, and hence doesn't recognize [[. And actually, you shouldn't use [[ ]] with a #!/bin/sh shebang anyway, since it's unsafe to depend on a feature that the shebang doesn't request.
So, what to do about it? You'll have the [ ] type of test expression available, but it doesn't do pattern matching (like *.txt). There are a number of alternate ways to do it:
The case statement is available in even basic shells, and has the same pattern matching capability as [[ = ]]. This is the most common way to do this type of thing, especially when you have a list of different patterns to check against.
More indirectly, you can use ${var%pattern} to try remove .txt from the end of the end of the value (see "Remove Smallest Suffix Pattern" here), and then check to see if that changed the value:
if [ "$i" != "${i%.txt}" ]
More explanation: suppose $i is "file.txt"; then this expands to [ "file.txt" != "file" ], so they're not equal, and the test (for !=) succeeds. On the other hand, if $i is "file.pdf", then it expands to [ "file.pdf" != "file.pdf" ], which fails because the strings are the same.
Other notes: when using [ ], use a single equal sign for string comparison, and be sure to properly double-quote all variable references to avoid confusion. Also, if you use anything that has special meaning to the shell (like < or >), you need to quote or escape them.
You could use the expr command's : operator to do regular expression matching. (Regular expressions are a different type of pattern from the basic wildcard or "glob" expression.) You could do this, but don't.
#!/bin/sh
for i in `ls`
do
if [[ "$i" = *".txt" ]] ; then
echo "$i is .txt file"
else
echo "$i is NOT .txt file"
fi
done
You don't have to loop in ls output, and sh implementation might vary among OS distributions.
Consider:
#! /bin/sh
for i in *
do
if [[ "$i" == *.txt ]]
then
echo "$i is txt file"
else
echo "$i is NOT txt file"
fi
done

What are these bash_profile functions doing?

I'm confused about this conditional:
if [[ ! -z "$1" ]]
What is this language?
Here is what I'm familiar with for my terminal and bash_profile:
Bash is the shell, or command language interpreter, for the GNU
operating system.
and
Simply put, the shell is a program that takes your commands from the
keyboard and gives them to the operating system to perform. In the old
days, it was the only user interface available on a Unix computer.
Nowadays, we have graphical user interfaces (GUIs) in addition to
command line interfaces (CLIs) such as the shell.
On most Linux systems a program called bash (which stands for Bourne
Again SHell, an enhanced version of the original Bourne shell program,
sh, written by Steve Bourne) acts as the shell program.
function parse_git_branch {
branch=`git rev-parse --abbrev-ref HEAD 2>/dev/null`
if [ "HEAD" = "$branch" ]; then
echo "(no branch)"
else
echo "$branch"
fi
}
function prompt_segment {
# for colours: http://en.wikipedia.org/wiki/ANSI_escape_code#Colors
# change the 37 to change the foreground
# change the 45 to change the background
if [[ ! -z "$1" ]]; then
echo "\[\033[${2:-37};45m\]${1}\[\033[0m\]"
fi
}
function build_mah_prompt {
# time
ps1="$(prompt_segment " \# ")"
# cwd
ps1="${ps1} $(prompt_segment " \w ")"
# git branch
git_branch=`parse_git_branch`
if [[ ! -z "$git_branch" ]]
then
ps1="${ps1} $(prompt_segment " $git_branch " 32)"
fi
# next line
ps1="${ps1}\n\$ "
# set prompt output
PS1="$ps1"
}
PROMPT_COMMAND='build_mah_prompt'
The language is Bash, a modern shell based on the old Bourne shell and
(mostly) compatible with POSIX standards.
test aka [
[[ is a Bash extension of the test command also known as [. The test command is a separate executable but since it’s so useful for shell programming, most (if not all) modern shells implement it as a shell builtin. The following commands show that both versions are available on many systems:
$ type -a test
test is a shell builtin
test is /usr/bin/test
$ type -a [
[ is a shell builtin
[ is /usr/bin/[
For more info, see man test or help test (with Bash).
[[
[[ is implemented as a Bash keyword (not an external command). It originally came from the Korn shell and works similarly but has many improvements over the original [ command. See the following for more info:
What is the difference between test, [ and [[ ?
What's the difference between [ and [[ in bash?
Is [[ ]] preferable over [ ] in bash scripts?
Specific example
According to man test (POSIX specification)
−z string True if the length of string string is zero; otherwise, false.
Thus, the [[ -z "$1" ]] construct returns 0 (value for True in Unix shells) if $1, the first positional parameter to a script or function is an empty string. Introducing the negation ! operator converts the expression to its Boolean opposite, i.e, False if the following expression evaluates to True and vice versa.
To sum up, the whole expression evaluates to True if the first argument to the function is a non-empty string and False if it’s empty (or possibly not set at all).
If you read the above links, you’ll notice that [[ ! -z "$1" ]] is actually equivalent to [[ -n "$1" ]] which return True if $1contains anything, i.e., is not empty. This can be further shortened to [[ $1 ]] as quotes aren’t required for variables within [[.
Note: the portable version (for POSIX shells) is [ -n "$1" ] or [ "$1" ] (where the variables have to be quoted to protect from pathname expansion, word splitting and other potential side effects). See http://mywiki.wooledge.org/Quotes for more info.
Functions
The remaining code are shell functions which look like they’re used to build up a colourful prompt which provides details of the status of a git repository if the current working directory is under version control.

logical OR existence test for shell variables under bash

I need to perform some logic based on if shell_var_1 OR shell_var_2 is set under bash.
If I use #!/bin/sh, I could just use:
if [ "$shell_var_1" -ne "0" -o "$shell_var_2" -ne "0" ] ; then
logic
fi
This just uses "-o" from test (http://unixhelp.ed.ac.uk/CGI/man-cgi?test)
However, if I specify #!/bin/bash, and want to use the '-v' option to test the shell variable's existence (added from bash 4.2 onwards), how'd I go about doing the same?
Ref: https://tiswww.case.edu/php/chet/bash/bash.html
if [ -v shell_var_1 -o -v shell_var_2 ] ; then
logic
fi
^ Is this considered correct? Am I not mixing bash and the test operator?
[[ $shell_var_1 || $shell_var_2 ]]
...is a very succinct and idiomatic way to write this in bash, compatible back to ancient releases, assuming that you consider a variable set to an empty value not to exist.
If you want a portable alternative which treats variables set to zero-byte values as unset, then
[ -n "$shell_var_1" ] || [ -n "$shell_var_2" ]
...will serve.
If you want a portable alternative that treats variables set to empty values as existing, then
[ -n "${shell_var_1+set}" ] || [ -n "${shell_var_2+set}" ]
...will do this.
If you really want to use -v (thus, treating variables set to empty values as existing, but being needlessly incompatible with POSIX shells and older bash releases -- including those shipped with MacOS), then use [[ ]] -- which makes it unmistakably clear to readers that you're using bash-only syntax, and isn't prone to some obscure bugs which test or [ (even the bash built-in form of them) are prone to when used in more than 2-arg form:
[[ -v shell_var_1 || -v shell_var_2 ]]

Bash script if statements

In Bash script, what is the difference between the following snippets?
1) Using single brackets:
if [ "$1" = VALUE ] ; then
# code
fi
2) Using double brackets:
if [[ "$1" = VALUE ]] ; then
# code
fi
The [[ ]] construct is the more versatile Bash version of [ ]. This is the extended test command, adopted from ksh88.
Using the [[ ... ]] test construct, rather than [ ... ] can prevent many logic errors in scripts. For example, the &&, ||, <, and > operators work within a [[ ]] test, despite giving an error within a [ ] construct.
More info on the Advanced Bash Scripting Guide.
In your snippets, there's no difference as you're not using any of the additional features.
[ is a bash builtin, [[ is a keyword. See the bash FAQ. Beware: most bash scripts on the internet are crap (don't work with filenames with spaces, introduce hidden security holes, etc.), and bash is much more difficult to master than one might think. If you want to do bash programming, you should study at least the bash guide and the bash pitfalls.
Using [[ supresses the normal wordsplitting and pathname expansion on the expression in the brackets. It also enables a number of addition operations, like pattern matching
Just in case portability is needed:
For portability testing you can get the Bourne shell via the Heirloom project or:
http://freshmeat.net/projects/bournesh
(On Mac OS X, for example, /bin/sh is no pure Bourne shell.)
which is also an external program, which doesn't mean that it isn't a builtin.
which [
/usr/bin/[
In single square brackets you have to use -lt for 'less than' alias < while else you could use <
if [ 3 -lt 4 ] ; then echo yes ; fi
yes
if [ 3 < 4 ] ; then echo yes ; fi
bash: 4: No such file or directory
if [[ 3 < 4 ]] ; then echo yes ; fi
yes
if [[ 3 -lt 4 ]] ; then echo yes ; fi
yes
4: No such file means, it tries to read from a file named "4" - redirecting stdin < The same for > and stdout.

Resources