Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.

In cross-platform, lowest-common-denominator sh you use:

#!/bin/sh value=`cat config.txt` echo "$value" 

In bash or zsh, to read a whole file into a variable without invoking cat:

#!/bin/bash value=$(<config.txt) echo "$value" 

Invoking cat in bash or zsh to slurp a file would be considered a Useless Use of Cat.

Note that it is not necessary to quote the command substitution to preserve newlines.

See: Bash Hacker's Wiki - Command substitution - SpecialitiesBash Hacker's Wiki - Command substitution - Specialities.

In cross-platform, lowest-common-denominator sh you use:

#!/bin/sh value=`cat config.txt` echo "$value" 

In bash or zsh, to read a whole file into a variable without invoking cat:

#!/bin/bash value=$(<config.txt) echo "$value" 

Invoking cat in bash or zsh to slurp a file would be considered a Useless Use of Cat.

Note that it is not necessary to quote the command substitution to preserve newlines.

See: Bash Hacker's Wiki - Command substitution - Specialities.

In cross-platform, lowest-common-denominator sh you use:

#!/bin/sh value=`cat config.txt` echo "$value" 

In bash or zsh, to read a whole file into a variable without invoking cat:

#!/bin/bash value=$(<config.txt) echo "$value" 

Invoking cat in bash or zsh to slurp a file would be considered a Useless Use of Cat.

Note that it is not necessary to quote the command substitution to preserve newlines.

See: Bash Hacker's Wiki - Command substitution - Specialities.

In cross-platform, lowest-common-denominator sh you use:

#!/bin/sh value=`cat config.txt` echo "$value" 

In bash or zsh, to read a whole file into a variable without invoking cat:

#!/bin/bash value=$(<config.txt) echo "$value" 

Invoking cat in bash or zsh to slurp a file would be considered a Useless Use of CatUseless Use of Cat.

Note that it is not necessary to quote the command substitution to preserve newlines.

See: Bash Hacker's Wiki - Command substitution - Specialities.

In cross-platform, lowest-common-denominator sh you use:

#!/bin/sh value=`cat config.txt` echo "$value" 

In bash or zsh, to read a whole file into a variable without invoking cat:

#!/bin/bash value=$(<config.txt) echo "$value" 

Invoking cat in bash or zsh to slurp a file would be considered a Useless Use of Cat.

Note that it is not necessary to quote the command substitution to preserve newlines.

See: Bash Hacker's Wiki - Command substitution - Specialities.

In cross-platform, lowest-common-denominator sh you use:

#!/bin/sh value=`cat config.txt` echo "$value" 

In bash or zsh, to read a whole file into a variable without invoking cat:

#!/bin/bash value=$(<config.txt) echo "$value" 

Invoking cat in bash or zsh to slurp a file would be considered a Useless Use of Cat.

Note that it is not necessary to quote the command substitution to preserve newlines.

See: Bash Hacker's Wiki - Command substitution - Specialities.

Removing unnecessary quotes.
Source Link
Alan Gutierrez
  • 16.2k
  • 1
  • 20
  • 17

In cross-platform, lowest-common-denominator sh you use:

#!/bin/sh value="`catvalue=`cat config.txt`"txt` echo "$value" 

In bash or zsh, to read a whole file into a variable without invoking cat:

#!/bin/bash value="$value=$(<config.txt)" echo "$value" 

Invoking cat in bash or zsh to slurp a file would be considered a Useless Use of Cat.

Note that it is not necessary to quote the command substitution to preserve newlines.

See: Bash Hacker's Wiki - Command substitution - Specialities.

In cross-platform, lowest-common-denominator sh you use:

#!/bin/sh value="`cat config.txt`" echo "$value" 

In bash or zsh, to read a whole file into a variable without invoking cat:

#!/bin/bash value="$(<config.txt)" echo "$value" 

Invoking cat in bash or zsh to slurp a file would be considered a Useless Use of Cat.

See: Bash Hacker's Wiki - Command substitution - Specialities.

In cross-platform, lowest-common-denominator sh you use:

#!/bin/sh value=`cat config.txt` echo "$value" 

In bash or zsh, to read a whole file into a variable without invoking cat:

#!/bin/bash value=$(<config.txt) echo "$value" 

Invoking cat in bash or zsh to slurp a file would be considered a Useless Use of Cat.

Note that it is not necessary to quote the command substitution to preserve newlines.

See: Bash Hacker's Wiki - Command substitution - Specialities.

Quote command to preserve newlines
Source Link
kynan
  • 13.7k
  • 6
  • 82
  • 82
Loading
Replaced dead link with Internet Archive link
Source Link
Loading
Put the cross-platform, lowest-common-denominator version first.
Source Link
Alan Gutierrez
  • 16.2k
  • 1
  • 20
  • 17
Loading
Source Link
Alan Gutierrez
  • 16.2k
  • 1
  • 20
  • 17
Loading