Hot answers tagged

99

The function keyword was introduced in ksh. The traditional Bourne shell only had the foo () syntax, and POSIX standardizes only the foo () syntax. In ATT ksh (but not pdksh), there are a few differences between functions defined by function and functions defined with the Bourne/POSIX syntax. In functions defined by function, the typeset keyword declares a ...


72

POSIX defines standard error as for writing diagnostic output This doesn't limit its use to error messages only. I would consider progress information as diagnostic output, so it belongs on standard error.


67

Why are POSIX mandatory utilities not built into shell? Because to be POSIX compliant, a system is required1 to provide most utilities as standalone commands. Having them builtin would imply they have to exist in two different locations, inside the shell and outside it. Of course, it would be possible to implement the external version by using a shell ...


65

It serves primarily as making sure the POSIX tool-chest is available both inside and outside a shell (see the POSIX rationale for requiring those). For cd, that is not tremendously useful but note that cd changes directories but has other side effects: it returns an exit status that helps determine whether you're able to chdir() to that directory or not, ...


61

It's due to the technical constraints of the time. The POSIX standard was created in the 1980s and referred to UNIX, which was born in the 1970. Several C compilers at that time were limited to identifiers that were 6 or 8 characters long, so that settled the standard for the length of variable and function names. Related questions: Why is 'umount&#...


59

Most important things POSIX 7 defines C API Greatly extends ANSI C with things like: more file operations: mkdir, dirname, symlink, readlink, link (hardlinks), poll(), stat, sync, nftw() process and threads: fork, execl, wait, pipe, semaphors sem_*, shared memory (shm_*), kill, scheduling parameters (nice, sched_*), sleep, mkfifo, setpgid() networking: ...


59

The latest (as of 2017) version of the POSIX spec for the rm utility is here (and the previous one there) and forbids the deletion of . and ... If either of the files dot or dot-dot are specified as the basename portion of an operand (that is, the final pathname component) or if an operand resolves to the root directory, rm shall write a diagnostic ...


51

"Useless use of cat" is more about how you write your code than about what actually runs when you execute the script. It's a sort of design anti-pattern, a way of going about something that could probably be done in a more efficient manner. It's a failure in understanding of how to best combine the given tools to create a new tool. I'd argue that stringing ...


48

Here is some code that does something different in POSIX sh and Bash: hello &> world Whether that is "invalid" for you I don't know. In Bash, it redirects both standard output and standard error from hello into the file world. In POSIX sh, it runs hello in the background and then makes an empty redirection into world, truncating it (i.e. it's ...


44

A complement to jasonwryan's great answer, addressing some of your issues: Your $XDG_CONFIG_HOME is not set to ~/. It simply isn't set. So applications that follow the XDG Specification use the default ~/.config The dirs inside /.config are not hidden because they don't have to. The whole point of using a ~/.config dir is to un-clutter the user's $HOME. ...


44

There is no difference AFAIK, other than the fact that the second version is more portable.


44

No, lower case names are not specified for software package installation directories. In fact, historically software packages installed in /opt started with the all-capitals stock ticker symbol of the company providing the package, such as SUNW for Sun Microsystems or ORCL for Oracle. So packages such as Sun's QFS filesystem would be installed in a ...


43

Ask locale: locale decimal_point This will output the decimal point using the current locale settings. If you need the thousands separator: locale thousands_sep


38

Unfortunately, 'portable' is usually a stronger requirement than 'POSIX-compliant' for shell scripts. That is, writing something that runs on any POSIX shell isn't too hard, but getting it to run on any real-world shell is harder. You can start by installing every shell in your package manager, in particular debian's posh sounds like what you want (Policy-...


38

On one conceptual level, the kernel is everything that runs at a "more privileged" level of hardware protection. That would be like ring 0 on x86 processors, system mode on ARM, kernel mode on MIPS, supervisor mode on 68xxx, etc. The kernel is usually interrupt-driven, either software interrupts (system calls) or hardware interrupts (disk drives, network ...


38

As far as I can tell, the use of -- as end-of-options-marker starts with sh and getopt in System III Unix (1980). According to this history of the Bourne Shell family, the Bourne Shell first appeared in Version 7 Unix (1979). But it didn't have a way for set to separate options from arguments. So the original Bourne shell could do: set -e - turn on exit-on-...


37

It's been available on Linux back into its prehistory. It is not POSIX, although many actual shells (including AT&T ksh and bash) will simulate it if it's not present in the OS; note that this simulation only works at the shell level (i.e. redirection or command line parameter, not as explicit argument to e.g. open()). That said, it should be available ...


35

Yes, there are differences. The most portable are test or [ ]. These are both part of the POSIX test specification. The if ... fi construct is also defined by POSIX and should be completely portable. The [[ ]] is a ksh feature that is also present in some versions of bash (all modern ones), in zsh and perhaps in others but is not present in sh or dash or ...


35

Because it's not useless. In the case of cat file | cmd, the fd 0 (stdin) of cmd will be a pipe, and in the case of cmd <file it may be a regular file, device, etc. A pipe has different semantics from a regular file, and its semantics are not a subset of those of a regular file: a regular file cannot be select(2)ed or poll(2)ed on in a meaningful way; ...


34

foo() any-command is the Bourne syntax supported by any Bourne-like shell but bash, yash and recent versions of posh (which only support compound commands). (the Bourne shell and AT&T implementations of ksh don't support foo() any-command > redirections unless any-command is a compound command though). foo() any-compound-command (examples of ...


33

No, it doesn't, mainly for the reason that it doesn't require systems to conform by default, or to comply to only the POSIX standard (to the exclusion of any other standard). For instance, Solaris (a certified compliant system) chose backward compatibility for its utilities in /bin, which explains why those behave in arcane ways, and provide POSIX-compliant ...


31

You can use ShellCheck (GitHub) as a linter for your shell scripts. There is also an online version. To detect POSIX compatibility issues (e.g. SC2039), the shebang line of your shell script should be #!/bin/sh. You can also pass --shell=sh to shellcheck. Example (test.sh): #!/bin/sh if [[ $HOSTNAME == test ]]; then echo fail &> foo fi Result (...


31

[ is synonym of the test command and it is simultaneously a bash builtin and separate command. But [[ is a bash keyword and works in some versions only. So for reasons of portability you are better off using single [] or test [ -w "/home/durrantm" ] && echo "writable"


28

You can find the HTML version of all the editions of POSIX 2008 online: original: http://pubs.opengroup.org/onlinepubs/9699919799.2008edition/utilities/rm.html TC1 (2013 edition) http://pubs.opengroup.org/onlinepubs/9699919799.2013edition/utilities/rm.html TC2 (2016 edition) http://pubs.opengroup.org/onlinepubs/9699919799.2016edition/utilities/rm.html That ...


27

No, there's no POSIX way, other than compiling a C program that does it. As a quick and dirty one: $ echo 'int main(int c,char*v[]){ execvp(v[1],&v[2]);perror(v[1]);return 127;}'>r.c && make r $ ./r ps zzz -f UID PID PPID C STIME TTY TIME CMD chazelas 7412 7411 0 10:44 pts/4 00:00:00 /bin/zsh chazelas 21187 7412 0 ...


27

Posix defines the standard streams thus: At program start-up, three streams shall be predefined and need not be opened explicitly: standard input (for reading conventional input), standard output (for writing conventional output), and standard error (for writing diagnostic output). When opened, the standard error stream is not fully buffered; the standard ...


27

The POSIX standard has a section with guidelines for conforming utilities (i.e., "such as those written specific to a local system or that are components of a larger application") that says Utility names should be between two and nine characters, inclusive. Utility names should include lowercase letters (the lower character classification) and digits only ...


27

There is no general solution, at least not if you need to support Linux, because the Linux kernel treats everything following the first “word” in the shebang line as a single argument. I’m not sure what NixOS’s constraints are, but typically I would just write your shebang as #!/bin/bash --posix or, where possible, set options in the script: set -o posix ...


26

No, that would be a bad idea. cat hugeregularfile.txt > /dev/null and touch -a hugeregularfile.txt are not the same. cat will read the whole file, even if you redirect the output to /dev/null. And reading the whole file might be exactly what you want. For example in order to cache it so that later reads will be significantly faster. The shell can't know ...


26

dr01 is right, but there's also another reason - usability. Back in the day, you didn't have something as comfortable as a keyboard to type on. If you were lucky, you had something akin to an old-school typewriter. If you were unlucky, you had to deal with systems that required actual physical work to operate (as in, it took a lot of force to press the "key")...


Only top voted, non community-wiki answers of a minimum length are eligible