Hot answers tagged

86 votes

Do progress reports/logging information belong on stderr or stdout?

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 ...
74 votes

Multiple arguments in shebang

Although not exactly portable, starting with coreutils 8.30 and according to its documentation you will be able to use: #!/usr/bin/env -S command arg1 arg2 ... So given: $ cat test.sh #!/usr/bin/...
  • 1,180
71 votes

What exactly is POSIX?

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 ...
67 votes

Why are POSIX mandatory utilities not built into the shell?

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 ...
  • 58.6k
65 votes
Accepted

Why are UNIX/POSIX system call namings so illegible?

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 ...
  • 26.6k
58 votes

How can I test for POSIX compliance of shell scripts?

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 ...
  • 2,834
53 votes

Why doesn't shell automatically fix "useless use of cat"?

"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 ...
  • 282k
48 votes

Is there any sh code that is not syntactically valid bash code?

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 ...
47 votes
Accepted

Which is the current decimal separator?

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 You can view all the numeric keywords ...
46 votes
Accepted

Multiple arguments in shebang

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 ...
44 votes
Accepted

Do progress reports/logging information belong on stderr or stdout?

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 (...
44 votes

Is it part of any standard (e.g. POSIX), that system files should be lower case?

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 ...
  • 3,477
40 votes
Accepted

When and how was the double-dash (--) introduced as an end of options delimiter in Unix/Linux?

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 ...
  • 1,196
36 votes

Why doesn't shell automatically fix "useless use of cat"?

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 ...
  • 29k
35 votes

Is test or [ or [[ more portable both between bash shells and between other shells?

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. ...
  • 221k
35 votes
Accepted

Does POSIX guarantee the paths to any standard utilities?

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 ...
34 votes
Accepted

What is the portable (POSIX) way to achieve process substitution?

That feature was introduced by ksh (first documented in ksh86) and was making use of the /dev/fd/n feature (added independently in some BSDs and AT&T systems earlier).  In ksh and up to ksh93u, it ...
33 votes
Accepted

How can I conditionally pass an argument from a POSIX shell script?

Modifying your original script: #!/bin/sh echo "Noise $1" echo "Enhancement $2" for snr in 0 5 10 15 20 25 do python evaluate.py --noise "$1" --snr "$snr" --iterations 1250 ${2:+--enhancement "$2"} ...
  • 282k
33 votes
Accepted

POSIX compliant way to redirect stdout and stderr to a file

You can check the POSIX specification. The language isn't always easy to follow, but if you know what you're looking for, you can see whether it's there or not. POSIX includes the redirection ...
32 votes
Accepted

Since when do the POSIX and GNU rm not delete /?

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....
31 votes
Accepted

Is test or [ or [[ more portable both between bash shells and between other shells?

[ 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 ...
  • 14.4k
31 votes
Accepted

What is a capital X in posix / chmod?

The manpage says: execute/search only if the file is a directory or already has execute permission for some user (X) POSIX says: The perm symbol X shall represent the execute/search ...
30 votes
Accepted

Ask for a password in POSIX-compliant shell?

read_password() { REPLY="$( # always read from the tty even when redirected: exec < /dev/tty || exit # || exit only needed for bash # save current tty settings: tty_settings=...
30 votes
Accepted

What conditions must be met for a file to be a text file as defined by POSIX?

Must a text file be a regular file? In the above excerpt it does not explicitly say the file must be a regular file No; the excerpt even specifically notes standard input as a potential text file. ...
28 votes
Accepted

Is there a POSIX way of setting zeroth argument of a target application?

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 &&...
27 votes
Accepted

Is it part of any standard (e.g. POSIX), that system files should be lower case?

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 ...
  • 282k
27 votes
Accepted

Why doesn't shell automatically fix "useless use of cat"?

The 2 commands are not equivalent: consider error handling: cat <file that doesn't exist> | less will produce an empty stream that will be passed to the piped program... as such you end up with ...
  • 386
26 votes

Why are UNIX/POSIX system call namings so illegible?

dr_ 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-...
  • 431
26 votes

Is "${PS1-}" valid syntax and how does it differ from plain "$PS1"?

The variable expansion ${parameter:-word} will use the value of $parameter if it's set and non-null (not an empty string), otherwise it will use the string word. Omitting the : will not test if the ...
26 votes

What's the POSIX-compliant way to get the epoch timestamp in a shell?

For the epoch time as an integer number of seconds, that would be: awk 'BEGIN{srand(); print srand()}' or: awk 'BEGIN{print srand(srand())}' As in POSIX awk, srand() without argument uses the ...

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