Skip to main content

All Questions

Tagged with
0 votes
2 answers
789 views

Bash and other shells $(...) remove the ending newline character [duplicate]

Why the shell structure $(...) removes the ending newline character? #!/bin/bash S='a b ' printf '%i [%s]\n' "${#S}" "$S" T=$(printf '%s' "$S") printf '%i [%s]\n' "...
Olivier Pirson's user avatar
2 votes
1 answer
465 views

if var='value' then in Zsh: Is it really a valid syntax without semicolon?

The following code works on Zsh 5.8 that I tried, despite the missing semicolon. But is it really a valid Zsh syntax? #!/bin/zsh if var='value' then echo 'then' fi Without an assignation that ...
Olivier Pirson's user avatar
48 votes
1 answer
13k views

What's wrong with var=$(</dev/stdin) to read stdin into a variable?

We've seen a few posts here lately that use this: var=$(</dev/stdin) in an attempt to read the shell's standard input into a variable. That is however not the correct way to do it on Linux-based ...
Stéphane Chazelas's user avatar
7 votes
3 answers
604 views

Is there any reason to avoid sourcing the shell's configuration file?

I recently read an answer here that included the following advice: However, as a rule, ⚠️you should not source ~/.zshrc. Depending on what's in your dotfiles, this can lead to all sorts of problems. ...
terdon's user avatar
  • 250k
19 votes
1 answer
2k views

How to use associative arrays safely inside arithmetic expressions?

A few Bourne-like shells support associative arrays: ksh93 (since 1993), zsh (since 1998), bash (since 2009), though with some differences in behaviour between the 3. A common use is for counting ...
Stéphane Chazelas's user avatar
5 votes
3 answers
494 views

What shells support "value substitution" like MirBSD Korn Shell?

On Android (which uses mksh, MirBSD Korn Shell), there's a special syntax of string substitution (called "value substitution"): ${|commands} Instead of collecting output of the commands (as ...
iBug's user avatar
  • 3,608
2 votes
2 answers
342 views

Which code works (with builtins) both in bash and zsh with the `time` keyword?

It is usual to do time cmd for many types of cmd(s) in bash: $ time true real 0m0.000s user 0m0.000s sys 0m0.000s The output format could be changed (even to a posix format with time -p): $...
user avatar
5 votes
2 answers
2k views

Is there a way to make the local command work portably to dash ksh bash and zsh?

In dash (and in bash zsh and some other shells) the command local works to make a variable scope limited to that function (and descendants in some cases). That enable the possibility of making a ...
user avatar
21 votes
1 answer
1k views

Is for(( ... )){ ... ;} a valid shell syntax? In which shells?

The expresion: $ ksh -c 'for ((i=0;i<10;i++)){ echo $i;}' 0 1 2 3 4 works correctly. I fail to find the description of this syntax in which the {...;} is replacing the ; do ...; done in the ...
user avatar
14 votes
3 answers
14k views

How to subtract two list (fast)? [duplicate]

What is a fast way to subtract two lists1. The lists may be small, maybe a direct way in shell works. Or the lists may be long, maybe external tools are the faster way. Assume you have two lists: ...
user avatar
6 votes
1 answer
1k views

Why does CDPATH not work as documented in the manuals?

The Bourne Again shell manual says, of cd dir: […] each directory name in CDPATH is searched for dir. […] If dir begins with a slash (/), then CDPATH is not used. The Z shell manual ...
JdeBP's user avatar
  • 71.2k
16 votes
9 answers
7k views

Check variable is an array in Bourne like shell?

In Bourne like shell which support array variable, we can use some parsing to check if variable is an array. All commands below were run after running a=(1 2 3). zsh: $ declare -p a typeset -a a a=(...
cuonglm's user avatar
  • 157k
6 votes
3 answers
725 views

Is "Arithmetic Expansion" the expected action on vars inside `[[` tests?

This script: a=2 [[ "$a" -eq 2 ]] && echo yes1 [[ $a -eq 2 ]] && echo yes2 [[ a -eq 2 ]] && echo yes3 [[ "a" -eq 2 ]] && echo yes4 Does not work in dash (and ...
user avatar
2 votes
2 answers
527 views

Reliably identifying the path to the directory containing the running script

There are plenty of answers in SO to address this question: Can a Bash script tell what directory it's stored in? Unix shell script to find out which directory the script resides? And there are a few ...
Amelio Vazquez-Reina's user avatar
0 votes
1 answer
55 views

Is there a valid way of exporting a variable across different shells?

For some reasons, Im using ksh and zsh in my scripts and returning to -bash, I need to use already set variable values in the spawned shells, writing it to some temp file and using that didn't look ...
Keyshov Borate's user avatar

15 30 50 per page