3

I usually work in bash in Linux, and if I need to export a variable I might write:

export LC_ALL=C

However, I am doing more Solaris work these days, and Solaris Admin's I work with will always write:

LC_ALL=C ; export LC_ALL

Is there a reason to prefer the latter over the former?

I guess that the former might not be recognised by some shells. Which ones?

Thanks.

6 Answers 6

5

I believe that true Bourne shell (/bin/sh on many commercial Unix variants) does not support the assignment and export in a single statement. This is how I remember it from my days on Solaris.

I don't know if the new Solaris versions use bash by default now. Regardless, your latter case is more portable so I'd stick with that.

2
  • Yup that's exactly it - this is neckbeard territory. If you are using a real bourne shell then you have to use two statements, otherwise with bash you can combine the two. Jan 19, 2010 at 4:27
  • Extra info: As of Solaris 11 (2011 onwards) the combined export command is supported by default, so no longer something to consider.
    – peterh
    Jan 16, 2017 at 15:23
3

In Solaris, /bin/sh requires the two statements, while /bin/bash is fine with it on one statement. On linux, /bin/sh is usually a symlink to /bin/bash, so there isn't a difference.

If you are writing a script which may be sourced by another script, then always use the two statement format. If the script isn't or won't be sourced, just check the shebang line at the top of the script to see which shell your using. Many Solaris scripts use /bin/sh, and this is the default for many software packages. As such most admin prefer the more portable way of exporting a variable.

1
  • Nowadays /bin/sh is typically a symlink to a dumbed down more strict shell called dash (This is definitely the case in all modern Debian distros). So like the first post states it is better to go with the more portable solution. However dash does seem to support the one line statement :)
    – ScottZ
    Jan 30, 2010 at 2:48
2

You can use the former if you are mainly working on bash since you can use bash as root shell without any dangerous side effects since Solaris 10. If the machine ever has to reboot into Single User and won't find bash theres an automatic fallback to Bourne Shell.

1

Any modern Bourne derived shell that I'm aware of supports the all-in-one form. I think older versions of the original Bourne shell may require the separate version.

1

I am working on Solaris 2.6, circa 1998. This version only came with sh and csh originally. Using sh, I see that you need the two line version you mention in order to see the correct result from env. Using bash from sunfreeware.org, and it works fine with the one line version.

0

In a Posix shell "typeset" can declare a variable, set a value, and export in a "single" statement.

typeset -x foo=bar

This behavior applies to Posix Shell, and Korn Shell.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .