Tag Info

Hot answers tagged

34

I'd guess lack of features - no command history, no fancy redirection, no command line editing. BSD introduced csh the C shell for those reasons. Another factor is that the Genuine Bourne Shell was only recently available in open source form. Unless you licensed it, you couldn't distribute it. That put it out of reach for free-of-cost distros, and made it ...


20

There's no real difference in behavior. Both commands do nothing and exit with a successful status. : emphasizes doing nothing; true emphasizes the successful status. strace true works because true is both a shell builtin and an external command (/bin/true); : is only a shell builtin (there's no /bin/: -- though there could be, and probably was on very ...


13

That's not the Bourne shell, or bash emulating the Bourne shell, that's the Almquist shell, in your case probably the Debian Almquist shell (a Linux fork by Debian of BSDs' sh itself based on the original Almquist shell). In the Almquist shell (the original one and the modern versions), % is used in PATH for extra features specific to ash. Quoting from the ...


12

The statement in your question is incorrect. Solaris up to version 10 is providing the legacy Bourne shell as /bin/sh. This was done not to break compatibility with old scripts that might fail with a different shell but was very frustrating otherwise. Most if not all remaining Unix and Unix like releases, including Solaris 11, do provide a POSIX compatible ...


11

As far as I know the original Bourne shell couldn't be used by the BSDs and the GNU project, because of its license. Back then the original Unix had no license and the GNU project needed a shell that was under the GPL so they used the bash. The same thing is true for BSD4 the father of all BSDs. Thanks to the AT&T lawsuit they needed to rewrite all ...


10

No, not all scripts intended for bash work with dash. A number of 'bashism' will not work in dash, such as C-style for loops and the double-bracket comparison operators. If you have a set of bash scripts that you want to use for dash, you may consider using checkbashisms. This tool will check your script for bash-only features that aren't likely to work in ...


10

SHELL SEQ: Probably a useful means of bench-marking a shell's performance is to do a lot of very small, simple evaluations repetitively. It is important, I think, not just to loop, but to loop over input, because a shell needs to read <&0. I thought this would complement the tests @Gnouc already posted because it demonstrates a single shell ...


9

No, if /bin/sh is a symlink to bash bash enters just posix mode - from man bash: When invoked as sh, bash enters posix mode after the startup files are read. If you now search for posix mode at the bash manpage you'll see that some shell builtins like time or source behave differently. That is all. bashishms like writing function before declaring a ...


7

The short answer is no, they're not 100% compatible. But most the shells are pretty close to the basic, so you would only rarely bump into inconsistencies. In fact, most shells differ not much by added syntax, but by some extra features like tab-completion and similar. Also, dash is sort of a descendant of ash - or port from BSD to linux, to be precise. ...


7

If you need a specific shell when running something from a cron job wrap it in a script and call the script from the cron. #!/bin/bash diff <(xzcat file1.xz) <(xzcat file2.xz) Cron entry * * * * * user-name /path/to/above/script.bash


6

You are running sh, which in Debian links to dash. If that is not the shell you want, try typing in exec bash. dash (well, neither dash nor the original Bourne sh) doesn't use readline, which explains why you see those escape sequences when you attempt to use the arrow keys.


6

There seems to be some confusion around the Bourne shell. The Bourne shell was Unix shell decades ago, in the pre-POSIX days. Nowadays "sh" is an implementation or another of a shell that implements the POSIX specification, among which we have bash, pdksh, AT&T ksh, newer Almquist shells and their derivatives that have been made POSIX compliant ...


6

In your first example, $((X+1)) evaluates the expression X + 1 and expands to the result, which is then asigned to the variable. In the second example, $((X=X+1)) increments X by one ($((X++)) is also valid and shorter, but not necessarily supported by the shell), then expands to the new value of $X, which is the first argument to the null command, :. The ...


6

They're identical in Bash. Look at builtins/colon.def in the Bash-4.2 source code. In your command strace true you are actually running the binary /bin/true instead of the bash built-in true.


6

The standard construct to define a function is run_backup () { … } In ksh, bash and zsh, but not in other shells such as dash, you can define a function with function run_backup { … } What happened when you ran the script with dash was: The shell executed the $(run_backup) line, naturally resulting in the first error message since there was no ...


6

-ne only means "not equal" when it's in an if [ … ] statement. In this case -ne is an option to echo. You could just as easily use -en. From bash(1): If -n is specified, the trailing newline is suppressed. If the -e option is given, interpretation of the following backslash-escaped characters is enabled. In this example there is no comparison. ...


5

Yes, process substitution is a non-standard feature originated in ksh and only available in ksh, bash and zsh. On systems that support /dev/fd/n (like Debian), you can do: xzcat < file1.xz | { xzcat < file2.xz | diff /dev/fd/3 -; } 3<&0 Or you can always do: bash -c 'diff <(xzcat file1.xz) <(xzcat file2.xz)'


4

Since you have super user access, you can just change /bin/sh. Of course you'll be affecting anything that wants to use the default shell (for example, cron scripts), so try to restore it as soon as possible. First, create the wrapper. Create in your home directory a file named mysh with this content: #!/bin/dash exec /bin/dash -x "$@" Make it ...


4

If you must use dash, this will work: mkfifo file1 mkfifo file2 xzcat file1.xz >file1& xzcat file2.xz >file2& diff file1 file2 rm -f file1 file2 #remove the FIFOs


4

Let do a benchmark. With bash: $ strace -cf bash -c 'for i in $(seq 1 1000); do bash -c ":"; done' % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 99.12 0.376044 188 2004 1002 wait4 0.74 0.002805 3 1002 clone 0.03 ...


3

Each shell has its own escape sequences for PS1, so you need to set it separately for each shell. Furthermore, PS1 only makes sense for interactive shells, it isn't used by other programs. So put PS1 in the interactive startup file for your shell: ~/.bashrc for bash ~/.kshrc for ksh ~/.zshrc for zsh Bash has a quirk: it doesn't load .bashrc in a login ...


3

Script arguments usually come after options. Take a look at any other commands such as cp or ls and you will see that this is the case. So, to handle: dash_script.sh -x -z -o OPTION FILE you can use getopts as shown below: while getopts xzo: option do case "$option" in x) echo "x";; z) echo "z";; o) echo "o=$OPTARG";; ...


3

You can test whether standard input is a terminal: if [ -n "$1" ]; then exec <"$1" elif tty >/dev/null; then echo 1>&2 'Cowardly refusing to read data from a terminal.' exit 2 # else we're reading from a file or pipe fi


3

This error message comes from ash. There are several shells with a similar syntax. Ash is a relatively basic one designed for a small memory footprint and fast execution. Another common shell is Bash. Bash has more features. The syntax you posted exist only in bash (and some other shells, but not ash). In ash, you would need to write¹: day=5 while [ $day ...


3

You could use the -x flag in the invoking command itself: sh -x ./script


3

It is linked to shells that provide 100% Backward compatibility. You don't lose anything that the original shell has but gain more features on top of it. There's no downside so why miss out on the new features? This is the same reason why /bin/vi is usually linked to vim.


3

Just put the code in a new file in /etc/profile.d/ and check that /etc/profile has some code that executes every script in that directory. My /etc/profile has: if [ -d /etc/profile.d ]; then for i in /etc/profile.d/*.sh; do if [ -r $i ]; then . $i fi done unset i fi which means the script needs to have a .sh extension


3

Dash is designed to be a minimal shell for fast script execution. Being comfortable for interactive use is very much not a design goal. If you want a fancy prompt, use a shell designed for interactive use: zsh, fish, or at least bash. The escape sequences you show are for bash. Dash does expand variables in the prompt, so you can set a prompt like this in ...


3

Lots of the commands in your .bashrc are bash-specific. Things like HISTCONTROL aren't relevant to dash. Likewise for the command completion. Aliases will work however. Read the manpage for dash and check the manpage for each thing you're doing in your .bashrc to see whether it's applicable to dash. The real question you're asking is not very clear: do you ...


3

You can just do: { commands .... } | logger -t my_awesome_script You can do that with any shell. If you don't like the way it looks, maybe make the script wrap itself in a function. #!/bin/sh run() if [ "$run" != "$$" ] then run=$$ exec "$0" "$@" | logger -t my-awesome-script fi #script-body run || do stuff



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