Comparing PHP version numbers using Bash?

Here’s how to compare versions. using sort -V: function version_gt() { test “$(printf ‘%s\n’ “$@” | sort -V | head -n 1)” != “$1”; } example usage: first_version=5.100.2 second_version=5.1.2 if version_gt $first_version $second_version; then echo “$first_version is greater than $second_version !” fi pro: solid way to compare fancy version strings: support any length of sub-parts … Read more

bash functions: enclosing the body in braces vs. parentheses

Why are braces used by default to enclose the function body instead of parentheses? The body of a function can be any compound command. This is typically { list; }, but three other forms of compound commands are technically allowed: (list), ((expression)), and [[ expression ]]. C and languages in the C family like C++, … Read more

Unable to have Bash-like C-x-e in Zsh

I’m using it with VIM mode. Basically ESC-v (or simply v if already in command mode) opens the terminal. It is setup by: autoload -U edit-command-line zle -N edit-command-line bindkey -M vicmd v edit-command-line Here is how to setup it in emacs mode: autoload edit-command-line zle -N edit-command-line bindkey ‘^Xe’ edit-command-line Use ‘bindkey -eā€™ to … Read more

Bash variable substitution vs dirname and basename

The external commands make some logical corrections. Check the result of the next script: doit() { str=$1 echo -e “string $str” cmd=basename [[ “${str##*/}” == “$($cmd $str)” ]] && echo “$cmd same: ${str##*/}” || echo -e “$cmd different \${str##*/}\t>${str##*/}<\tvs command:\t>$($cmd $str)<” cmd=dirname [[ “${str%/*}” == “$($cmd $str)” ]] && echo “$cmd same: ${str%/*}” || echo … Read more

how to make bash expand wildcards in variables?

A deleted answer was on the right track. A slight modification to your attempt: shopt -s extglob MYDIR=”./images” OTHERDIR=”./images/junk” SUFFIXES=’@(pdf|eps|jpg|svg)’ mv “$MYDIR/”*.$SUFFIXES “$OTHERDIR/” Brace expansion is done before variable expansion, but variable expansion is done before pathname expansion. So the braces are still braces when the variable is expanded in your original, but when the … Read more

What does the colon do in PATH

: is the separator. The PATH variable is itself a list of folders that are “walked” through when you run a command. In this case, the folders on your PATH are: /Users/chengluli/anaconda/bin /Users/chengluli/.rbenv/shims /

Redirecting bash stdout/stderr to two places?

You can use a named pipe, which is intended for exactly the situation you describe. mkfifo some_pipe command_that_writes_to_stdout | tee some_pipe \ & command_that_reads_from_stdin < some_pipe rm some_pipe Or, in Bash: command_that_writes_to_stdout | tee >(command_that_reads_from_stdin)

bash flock: Why 200?

Theres nothing special about the number 200. It just happens to be the example used in the man page of the flock command; and it happens to be a large number, so it’s unlikely to conflict with the the file descriptor of any other file you open during your script. In your comment, you ask … Read more