As of May 31, 2023, we have updated our Code of Conduct.

Hot answers tagged

282 votes
Accepted

Split string by delimiter and get N-th element

Use cut with _ as the field delimiter and get desired fields: A="$(cut -d'_' -f2 <<<'one_two_three_four_five')" B="$(cut -d'_' -f4 <<<'one_two_three_four_five')" You can also use ...
heemayl's user avatar
  • 54.2k
92 votes

How to color diff output?

diff --color option was added to GNU diffutils 3.4 (2016-08-08) This is the default diff implementation on most distros, which will soon be getting it. Ubuntu 18.04 has diffutils 3.6 and therefore has ...
Ciro Santilli OurBigBook.com's user avatar
74 votes
Accepted

How to get the current date and time in YYYYMMDDHHMMSS format in ksh88?

I believe this is the command you need: date '+%Y%m%d%H%M%S'
SparedWhisle's user avatar
  • 3,568
57 votes
Accepted

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

(and no, for once, this is not about the missing quotes around $(...)¹). The $(<file) operator That Korn shell operator (also supported by zsh and bash) is described at length at Understanding Bash&...
Stéphane Chazelas's user avatar
56 votes

Split string by delimiter and get N-th element

Wanted to see an awk answer, so here's one: A=$(awk -F_ '{print $2}' <<< 'one_two_three_four_five') B=$(awk -F_ '{print $4}' <<< 'one_two_three_four_five') Try it online!
Paul Evans's user avatar
42 votes

Preferred syntax for two lines long pipe

Ask your self what would this do? command1 \ | command2 Can't see the difference. Neither can I, but the shell can. Look closely, there is a space after the \. This stops the newline from being ...
ctrl-alt-delor's user avatar
40 votes

Split string by delimiter and get N-th element

Using only POSIX sh constructs, you can use parameter substitution constructs to parse one delimiter at a time. Note that this code assumes that there is the requisite number of fields, otherwise the ...
Gilles 'SO- stop being evil''s user avatar
36 votes

Is the shell ksh93 dead?

It is not possible to give a real answer to this question, but the form of a comment is not sufficient. So I think it may be a good idea to collect points to a editable answer... Two years ago, David ...
schily's user avatar
  • 18.6k
29 votes

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

{ ...; } was already accepted in place of do ...; done in for loops in the first version of the Bourne shell in Unix V7 in the late 70s (see the source code) though never documented as far as I know. ...
Stéphane Chazelas's user avatar
28 votes

How to color diff output?

If you have access to GNU diff you can use its --X-group-format options to get that effect without any additional tools: diff --old-group-format=$'\e[0;31m%<\e[0m' \ --new-group-format=$'\e[0;...
Michael Homer's user avatar
28 votes

What does //1/ mean

For the most part, multiple slashes are equivalent to a single slash. There's one exception: paths beginning with exactly two slashes (//foo/…, as opposed to /foo/… or ///foo/…) have a different ...
Gilles 'SO- stop being evil''s user avatar
23 votes

Find directories that do not contain subdirectories

If you are able to use find and if you are working on a "normal Unix filesystem" (that is, as defined in find(1) under -noleaf option description), then the following command can be used: find . -...
intelfx's user avatar
  • 5,041
23 votes

Delete First line of a file

This topic is interest, so I test the benchmark in 3 ways: sed '1d' d.txt > tmp.txt tail -n +2 d.txt > tmp.txt sed -i '1d' d.txt Note that target d.txt is 5.4GB file Get the result : run 1 ...
waue0920's user avatar
  • 331
23 votes
Accepted

Is the shell ksh93 dead?

NO tldr: github.com/att/ast and github.com/att/uwin On Jan 19-20, 2016 the following (1|2) messages were posted to the ast-users mailing-list: (and I consider the dgk has some patches comment ...
mikeserv's user avatar
  • 57.2k
23 votes
Accepted

How to use associative arrays safely inside arithmetic expressions?

The problem is that in a shell arithmetic expression, such as inside $((...)) (POSIX), or ((...)) (ksh/bash/zsh), or array indices or arguments of some shell builtins or [[...]] operands, word ...
Stéphane Chazelas's user avatar
21 votes
Accepted

exit trap in dash vs ksh and bash

zsh, pdksh (though not recent versions of mksh derived from that), yash, the Bourne shell behave like dash. Only bash, ksh88, ksh93 and mksh behave otherwise. The POSIX spec is not clear on what ...
Stéphane Chazelas's user avatar
19 votes
Accepted

Equivalent to "source" in OpenBSD?

You are using the Forsyth PD Korn shell, the usual login shell on OpenBSD. The PD Korn shell does not have a source command. The source built-in command is only available in some shells. The ...
JdeBP's user avatar
  • 66k
17 votes
Accepted

KSH/BASH Maximum size of an Array

i=0 while true; do a[$i]=foo i=$((i+1)) printf "\r%d " $i done This simple script shows on my systems (Gnu/Linux and Solaris): ksh88 limits the size to 2^12-1 (4095). (subscript out of ...
jlliagre's user avatar
  • 60k
16 votes

telnet command with custom timeout duration

This will run for no more than 2 secounds: []# echo quit | timeout --signal=9 2 telnet [SERVER] [PORT]
user286146's user avatar
16 votes

Preferred syntax for two lines long pipe

I'm going to disagree with most folks here; I always prefer to wrap before a joining operator such as a pipe: command1 \ | command 2 (You don't need to indent the second line; the pipe itself links ...
gidds's user avatar
  • 261
14 votes
Accepted

What does "set -f" in Korn Shell do?

set -f is the portable (i.e. POSIX) way to disable filename expansion. When enabled (by default or with set +f), filename expansion is an operation performed by the shell that replaces, when possible,...
jlliagre's user avatar
  • 60k
13 votes

Why does parameter expansion with spaces without quotes work inside double brackets "[[" but not inside single brackets "["?

When should I assign double quotes around variables like "${var}" to prevent problems caused by spaces? Implicit in this question is Why isn’t ${variable_name} good enough? ${...
G-Man Says 'Reinstate Monica''s user avatar
13 votes

How to use and/or conditional in shell script

The standard (POSIX sh and utilities) canonical legible ways would be: string comparison: if [ "$a" != 0 ] || [ "$b" != 100 ]; then... decimal integer comparison (0100 is 100, whether leading ...
Stéphane Chazelas's user avatar
13 votes

Is the shell ksh93 dead?

Yes and no. The official AT&T Korn Shell may be pining for the fjords, but there are two actively developed clones. There's pdksh, the public domain clone of the Korn shell, but that hasn't been ...
oals's user avatar
  • 361
13 votes
Accepted

How to subtract two list (fast)?

You can use comm to remove anything that’s common to both lists: listr=($(comm -3 <(printf "%s\n" "${list1[@]}" | sort) <(printf "%s\n" "${list2[@]}" | sort) | sort -n)) This sorts both lists ...
Stephen Kitt's user avatar
13 votes

Are Here-Strings available in ksh88?

No, here strings are not available on ksh88 and pdksh. On the more recent ksh93 (original AT&T Korn Shell) and mksh (currently actively developed pdksh derivative) it is, however, available. <&...
mirabilos's user avatar
  • 1,715
13 votes

How to put a string search with grep command into if statement?

Another option: grep -qw -- "$users" "$file1"; in_file1=$? grep -qw -- "$users" "$file2"; in_file2=$? case "${in_file1},${in_file2}" in 0,0) echo found in both files ;; 0,*) echo only in ...
glenn jackman's user avatar
12 votes

How to color diff output?

Try colordiff file1 file2 Availability of colordiff with your Linux/BSD distribution Those running Debian or Ubuntu (or any of their derivatives) can probably just use "apt-get install colordiff&...
modesto's user avatar
  • 231
12 votes

check if date argument is in yyyy-mm-dd format

This will check for the correct format (YYYY-MM-DD) in bash (with built-in regex match): if [[ $1 =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]] then echo "Date $1 is in valid format (YYYY-MM-DD)" else echo "...
A.P.'s user avatar
  • 1,406

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