Differences between revisions 18 and 19
Revision 18 as of 2009-10-11 16:39:21
Size: 6880
Editor: NeilMoore
Comment: echo and printf
Revision 19 as of 2009-10-11 16:44:23
Size: 7180
Editor: NeilMoore
Comment: local
Deletions are marked like this. Additions are marked like this.
Line 85: Line 85:
 * `local` -- there is no posix equivalent. You can use `$funcname_varname` to reduce the likelihood of conflicts, but even that is not enough for recursive functions. In posix only the positional parameters are truly local, so recursive functions should pass all "local" variables as parameters.

How to make bash scripts work in dash

This page is an attempt to list some of the most common bashisms, i.e. features not defined by POSIX (won't work in dash, or general /bin/sh). It probably won't be exhaustive. Note also we talk about "bashism" because this wiki is largely bash-centric but a number of these extensions work in other shells like ksh or zsh.

Syntax

Works in bash

Change to for dash

Comment

defining functions

function f(){echo hello world}

f() { echo hello world ; }

"function" is not defined by posix, only "name ()" is

case

;;& ;& etc

none. duplicate the case (use a funnction to avoid code duplication)

;;& ;& in bash4 is not defined by posix

numeric C-like for loop

for ((i=0; i<3; i++)); do echo $i ; done

i=0 ; while test $i -lt 3 ; do echo $i ; i=$(($i+1)) ; done

this syntax is not defined by posix

expand sequences

echo $'hello\tworld'

printf "hello\tworld"

$' ' is not defined by posix

extended glob

+( ) @( ) !( ) *( )

not always possible, some times you can use several globs, sometimes you can use find(1)

not defined by posix

select

select

some ideas: implement the menu yourself, use a command like dialog

select is not defined by posix

Expansions

  • Brace Expansion, eg {1..10} is not defined by posix
  • <( ) >( ) process substitution is not defined by posix, but can be simulated with FIFOs: instead of foo <(bar), write mkfifo fifo; bar > fifo & foo fifo (this is basically how process substitution is implemented on OSes that don't have a mechanism like /dev/fd/ to refer to unnamed pipes with filenames).

Parameter Expansions

List of expansions not defined by posix:

  • ${name:n:l} -- if the variable contains no newlines, you can use $(printf '%s\n' "$name" | cut -c $n-$((n+l-1))). If there are newlines, you can use $(printf %s "$name" | dd bs=1 skip=$n count=$l 2>/dev/null), but printf may report a (harmless) broken pipe if the variable contents are large enough.

  • ${name/ } -- you can use $(printf '%s\n' "$name" | sed 's/foo/bar/'), after changing shell patterns to regular expressions.

  • ${!name} -- it is possible, but dangerous, to use eval to achieve similar effects; see BashFAQ/006.

Note that using $( ) has the side-effect of removing trailing newlines from the results. Furthermore, since many standard Posix utilities, such as sed, require text files as input, you should ensure their input ends in a newline.

Arrays

Arrays are not defined by posix, there is no easy general workaround for arrays. Here are some hints:

  • The positional parameters are a kind of array (only one array):

#build a command dynamically see [[BashFAQ/050]]
set -- 'mycommand' 'needs some complex' 'args'
"$@"
#access the i'th param
set -- one two three
i=2
eval var=\$$i #take care if i comes from some user input see below
echo "$var"

Conditionals

Works in bash

Change to for dash

Comment

simple test

[[

use [ and use double quotes around the expansions [ "$var" = "" ]

[[ is not defined by posix

pattern matching

[[ foo = *glov ]]

use case or grep

see BashFAQ/041

equality with test

==

use = instead

only = is defined by posix, = works also in bash

compare lexicographically.

< >

no change

works with dash but not defined by posix a possible workaround with awk: awk -v v1="1" -v v2="fcd" 'BEGIN{exit !(v1 "" < "" v2)}'

compare modification times

[[ file1  -nt file2 ]] or -ot`

[ "$(find 'file1' -prune -newer 'file2')" ]

-prune is required to avoid recursion

check if 2 files are the same hardlink

[[  file1 -ef file2 ]]

?

-et is not defined by posix

(( ))

(( )) (without the $) acts like a command on its own

For simple comparison: [ -lt ] (and -ne -gt -ge) or [ "$(( 3+1 < 5))" -eq 0 ].

To assign a variable var=$((3+1))

Arithmetic

Works in bash

Change to for dash

Comment

pre/pos increment/decrement

++ --

i=$(($i+1))

-

-

let

: $((i=4+1))

The : command can be used to peform side effects with an expansion

Redirections

Works in bash

Change to for dash

Comment

redirect both stdout and stderr

>& and &>

command > file 2>&1 or commnd 2>&1 | othercommand

-

|& (bash4)

command 2>&1 | othercommand

-

duplicate and close

m>&n- m<&n-

m>&m n>&-

not defined by posix

Builtins

  • echo -n or -e -- posix doesn't define any options, and furthermore allows echo -e to be the default behavior. Instead use printf "%s\n" (for normal echo) or printf "%b\n" (for echo -e); leave off the \n to simulate echo -n.

  • printf -v is not defined by posix. Also the %q format is not defined by posix

  • read -- the only option defined by posix is -r

  • shopt, and therefore all the options it provides (extglob, nullglob, dotglob, etc.) are not defined by posix

  • local -- there is no posix equivalent. You can use $funcname_varname to reduce the likelihood of conflicts, but even that is not enough for recursive functions. In posix only the positional parameters are truly local, so recursive functions should pass all "local" variables as parameters.

Special Variables

Works in bash

Change to for dash

Comment

keep track of the times

SECONDS

before=$(date +%s) ....seconds=$(( $(date +%s) - $before))

date +%s is not posix see this faq for more info

Generate a random number

RANDOM

random=$(awk 'BEGIN{srand(); printf "%d\n",(rand*256)}') gives a number between 0 and 256

Be sure to learn what srand() and rand() do, ie this method fails if you call awk several times rapidly. Instead generate all the numbers you need inside awk. Some systems also provides /dev/random and /dev/urandom

Get the status of all the commands in a pipeline

PIPESTATUS

Simplest solution mkfifo fifo; command2 <fifo & command1 >fifo; echo $? see NamedPipes

see this faq and this script pipe status for posix shell

More

  • The bash manual has a list of the differences between bash running in posix mode and a normal bash.

Note that bash in posix mode is only guaranteed to run a shell written according to the posix specification. It doesn't mean that it will fail if you use bashisms in your scripts.


CategoryShell

Bashism (last edited 2022-10-20 23:13:29 by larryv)