Skip to main content
304 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
  • 56.7k
104 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
64 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
44 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
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
29 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
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
26 votes
Accepted

How to use associative arrays safely inside arithmetic expressions?

The problem is that in a shell arithmetic expression, such as inside $((...)) (POSIX), ((...)) (ksh/bash/zsh) array indices some arguments of some shell builtins operands of numeric comparison ...
Stéphane Chazelas's user avatar
18 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
  • 69.5k
17 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
17 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
  • 271
15 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
  • 61.6k
15 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
14 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 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
  • 241
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,752
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 does ksh93 avoid forks in command substitution

Ksh93 does a lot to avoid forks. I have no idea how it knows how to handle the first case, as a truss shows that it only calls one write(2) call with the final result. It may be that David scans the ...
schily's user avatar
  • 19.3k
12 votes
Accepted

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

Try this: if grep -wq -- "$user" "$file1" && grep -wq -- "$user" "$file2" ; then echo "string avail in both files" elif grep -wq -- "$user" "$file1" "$file2"; then echo "string avail in ...
Siva's user avatar
  • 9,077
12 votes

Preferred syntax for two lines long pipe

Well, just to avoid it looking like nobody would prefer: command1 \ | command2 I'm going to say that I do. I see the trailing space problem raised by ctrl-alt-delor as a non-issue. Editors can ...
JoL's user avatar
  • 4,795
12 votes
Accepted

Why does ksh93 not support the %T format specifier of its built-in printf in AIX?

%T was added to ksh's printf in ksh93h released in 1999 Note the h suffix (where you have e) which indicates the version of ksh93. The latest release of ksh93 by AT&T was ksh93u+ released in 2012. ...
Stéphane Chazelas's user avatar
11 votes
Accepted

typeset in ksh93 not working as expected

typeset is ksh93's private (using static scoping like perl's my, not local which does dynamic scoping) only for functions that are declared using the ksh function definition style: function foo { ...
Stéphane Chazelas's user avatar
11 votes

Script throwing error "=: not found"

Variable assignments must not include $ and spaces around the =. I also would double quote the assignment. So the variable assignment should look like as follows. a="/is115/idm/dsproj/scripts/l.txt" ...
Thomas's user avatar
  • 6,442
10 votes

How to color diff output?

if you have vim installed, you can do diff file1 file2 | vim - Vim will recognise diff format and give it proper coloring. The dash at the end is to let vim accept input from diff command.
TrongBang's user avatar
  • 201
10 votes

telnet command with custom timeout duration

You can consider the neater command netcat aka nc as discussed here echo 'QUIT' | nc -w SECONDS YOUR_HOST PORT; echo $?
Nam G VU's user avatar
  • 511
10 votes

Equivalent to "source" in OpenBSD?

The source keyword which is available in bash is not part of the Posix standard. Instead you can use . myVenv/bin/activate You could use the same syntax with . in bash which you are using on your ...
Bodo's user avatar
  • 6,088
10 votes
Accepted

Migrate shells Solaris ksh to Red Hat

Your (internal) RedHat system administrators will be able to do this for you. It's a trivial operation, something like this yum install ksh The system default shell and the shell used to run your ...
Chris Davies's user avatar

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