2022 Developer Survey is open! Take survey.

Hot answers tagged

252 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 ...
user avatar
  • 50.6k
73 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 ...
user avatar
60 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'
user avatar
  • 3,258
51 votes
Accepted

Are there problems with hyphens in functions, aliases, and executables?

POSIX and Hyphens: No Guarantee According to the POSIX standard, a function name must be a valid name and a name can consist of: 3.231 Name In the shell command language, a word consisting ...
user avatar
  • 69.2k
49 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!
user avatar
41 votes

How to get a response from any URL?

curl -Is http://www.yourURL.com | head -1 You can try this command to check any URL.Status code 200 OK means that the request has succeeded and the URL is reachable. You can also test URL ...
user avatar
  • 8,828
41 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 ...
user avatar
37 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 ...
user avatar
  • 17.9k
37 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 ...
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. ...
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 ...
user avatar
26 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;...
user avatar
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 ...
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 ...
user avatar
  • 55.6k
21 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 . -...
user avatar
  • 4,707
20 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 ...
user avatar
20 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 ...
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 ...
user avatar
  • 62.1k
17 votes
Accepted

Using Diff on a specific column in a file

awk is a better tool for comparing columns of files. See, for example, the answer to: compare two columns of different files and print if it matches -- there are similar answers out there for printing ...
user avatar
16 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 ...
user avatar
  • 58k
15 votes
Accepted

awk + print lines from the first line until match word

Try: $ awk '1;/PPP/{exit}' file AAA BBB JJJ OOO 345 211 BBB OOO OOO PPP
user avatar
  • 143k
15 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 ...
user avatar
  • 251
14 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]
user avatar
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,...
user avatar
  • 58k
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 ...
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 ...
user avatar
  • 341
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. <&...
user avatar
  • 1,571
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 ...
user avatar
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 "...
user avatar
  • 1,376
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 ...
user avatar
  • 17.9k

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