[SOLVED] Where I can find the ash syntax documentation

Hi,
I'm trying to write scripts in ash but are having a lot of problems with syntax.
Things that are correct in other linux distributions, are giving me syntax errors in ash.
For example:
x='ABCDEF'
echo ${x:(-4)}
Gives -ash: syntax error: bad substitution
I need the last 4 characters.
Can anyone indicate a good ash syntax documentation.
Best regards,
carlied

(SOLVED)
Without () it works.
WRONG: echo ${x:(-4)}
CORRECT: echo ${x:-4}

1 Like

Anything POSIX should work AFAIK.

ash is very close to POSIX sh or dash. https://linux.die.net/man/1/dash is one helpful man page, as would be https://www.freebsd.org/cgi/man.cgi?query=sh&sektion=&n=1

Many scripts found on the Internet assume bash is the shell. bash has many differences in behavior from sh and bash scripts often fail when run under sh/ash/dash.

https://wiki.ubuntu.com/DashAsBinSh describes many of the differences

One of the "challenges" with portable scripting is around the echo built-in, especially when it comes to suppressing the newline. Using printf can make scripts more robust.

4 Likes

/bin/sh is a symlink to /bin/ash, which in turn points to- and is provided by busybox on OpenWrt. You can install (if it isn't already preinstalled for initramfs purposes) busybox on most linux distributions and override your script to use it ("#!/.../busybox sh" or invoking it through busybox explicity).

In general busybox sh is more POSIXly strict than bash, but less than dash, pdksh, mksh, posh, ... so testing with dash is also a valid alternative, which should catch most bashisms.

1 Like

Thanks slh, but that is not an option in my case because I have no more space on my flash.

Thanks jeff. I will look for it.

I was not suggesting to install additional shells on your router, but on a regular linux distribution (debian, fedora, opensuse, ubuntu, ...) to test your scripts there.

But if you're looking for specifications, the closes will probably be SUSv4, with Shell Command Language of SUSv4 in particular.

thank you anyway

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.