You are not logged in.

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

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

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: Scotland
Registered: 2010-06-16
Posts: 11,863
Website

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: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Offline

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

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

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