You are not logged in.

#1 2013-03-13 15:29:43

jakobcreutzfeldt
Member
Registered: 2011-05-12
Posts: 1,033

How portable are shell parameter expansions?

I'm finding that the simplest solution to a problem in a Makefile that I'm writing is to use the ${var#param} parameter expansion. How portable is this? I've tried using both bash and sh and they both work as expected, but I'd like to make sure that this is a standard feature present in all relevant shells.

$ bash -c 'var="Hello";/usr/bin/echo ${var#Hell}'
o
$ sh -c 'var="Hello";/usr/bin/echo ${var#Hell}'
o

Last edited by jakobcreutzfeldt (2013-03-13 15:44:03)

Offline

#2 2013-03-13 15:33:14

WorMzy
Forum Moderator
From: England
Registered: 2010-06-16
Posts: 5,019

Re: How portable are shell parameter expansions?

fish looks problematic, but zsh and dash are fine:

$ zsh -c 'var="Hello";/usr/bin/echo ${var#Hell}'
o
$ dash -c 'var="Hello";/usr/bin/echo ${var#Hell}'
o
$ fish -c 'var="Hello";/usr/bin/echo ${var#Hell}'
fish: Unknown command “var=Hello”. Did you mean “set var Hello”? For information on assigning values to variables, see the help section on the set command by typing “help set”.
Standard input: var="Hello";/usr/bin/echo ${var#Hell}
                ^
fish: Could not expand string “${var”
Standard input: var="Hello";/usr/bin/echo ${var#Hell}
                                          ^

Sakura:-
Mobo: ASUS P8Z77-V PRO // Processor: Intel Core i7-3770K 3.4GHz // GFX: nVidia GeForce GTX 970 Ti // RAM: 32GB (4x 8GB) Corsair DDR3 (@ 2133MHz) // Storage: 1x 3TB Seagate SATAII 5x 1TB Samsung SATAII, 2x 120GB Corsair SSD

Offline

#3 2013-03-13 15:46:45

jakobcreutzfeldt
Member
Registered: 2011-05-12
Posts: 1,033

Re: How portable are shell parameter expansions?

Ok thanks. It seems that fish isn't Bourne-compatible (given the way it handles vars).

Offline

Board footer

Powered by FluxBB