Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

6 Answers 6

up vote 5 down vote accepted

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.

share|improve this answer
    
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. –  Phil Hollenback Jan 19 '10 at 4:27

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.

share|improve this answer
    
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 '10 at 2:48

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.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.