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.
Related Posts:
- What does the line “#!/bin/sh” mean in a UNIX shell script?
- Shell script “for” loop syntax
- Difference between sh and Bash
- How do I pause my shell script for a second before continuing?
- How to pretty print XML from the command line?
- In the shell, what does ” 2>&1 ” mean?
- What are file descriptors, explained in simple terms?
- Ubuntu says “bash: ./program Permission denied” [closed]
- In the shell, what does ” 2>&1 ” mean?
- How to kill a process running on particular port in Linux?
- Display exact matches only with grep
- How can I check if a directory exists in a Bash shell script?
- OS X: equivalent of Linux’s wget
- How to split a delimited string into an array in awk?
- OS X: equivalent of Linux’s wget
- Display exact matches only with grep
- How can I declare and use Boolean variables in a shell script?
- How can I declare and use Boolean variables in a shell script?
- Shell Script Syntax Error: Unexpected End of File
- What does ^M character mean in Vim?
- Who can access a file with octal permissions “000” on Linux/UNIX?
- What does it mean to write to stdout in C?
- What do ‘real’, ‘user’ and ‘sys’ mean in the output of time(1)?
- How to cat <
> a file containing code? - mkdir’s “-p” option
- Given two directory trees, how can I find out which files differ by content?
- Which characters need to be escaped when using Bash?
- wait(null) and wait(&status) C language and Status
- Diff command along with Grep gives “Binary file (standard input) matches”
- How to get the current directory in a C program?
- When could or should I use chmod g+s on a file or directory?
- How to Sum a column in AWK?
- Equivalent of *Nix ‘which’ command in PowerShell?
- What is the difference between “#!/usr/bin/env bash” and “#!/usr/bin/bash”?
- What is the difference between SIGSTOP and SIGTSTP?
- How to colorize diff on the command line
- How to colorize diff on the command line
- Badly placed ()’s error with the following shell script
- Command to change the default home directory of a user
- Is there any simple way to benchmark Python script?
- Sorting data based on second column of a file
- gpg decryption fails with no secret key error
- Variable interpolation in the shell
- What is the difference between tar and zip?
- Display current path in terminal only
- What does “&” at the end of a linux command mean?
- What does set -e mean in a bash script?
- What does set -e mean in a bash script?
- Syntax error near unexpected token ‘then’
- What do the dup() and dup2() systems really do?
- Shell script not running, command not found
- Using grep and ls -a commands
- What generates the “text file busy” message in Unix?
- source command not found in sh shell
- Reasoning behind C sockets sockaddr and sockaddr_storage
- What are the uses of the exec command in shell scripts?
- How can I send an email through the UNIX mailx command?
- Rename multiple files based on pattern in Unix
- What is the difference between a symbolic link and a hard link?
- SCP Permission denied (publickey). on EC2 only when using -r flag on directories
- binary operator expected error when checking if a file with full pathname exists
- What is special about /dev/tty?
- How do I do ‘mount –bind’ in /etc/fstab?
- How to read backward from the end of file in less or more?
- What does ‘set -e’ do, and why might it be considered dangerous?
- Getting the last match in a file using grep
- How to use execvp()
- How to use execvp() to execute a command
- What is a reverse shell?
- Meaning of “! -S” in shell script
- “No such file or directory” but it exists
- How do I split a string on a delimiter in Bash?
- What’s a .sh file?
- What’s a .sh file?
- How to execute a program or call a system command?
- What is newline character — ‘\n’
- Why does ENOENT mean “No such file or directory”?
- sudo: npm: command not found
- Extract substring in Bash
- How do I grep recursively?
- Where to place $PATH variable assertions in zsh?
- How do I grep recursively?
- Should I put #! (shebang) in Python scripts, and what form should it take?
- Should I put #! (shebang) in Python scripts, and what form should it take?
- What is the meaning of *nix?
- How do I use a regex in a shell script?
- How do I copy a folder from remote to local using scp?
- How to concatenate string variables in Bash
- -bash: syntax error near unexpected token `)’
- How to grep for case insensitive string in a file?
- An example of how to use getopts in bash
- Difference between wait and sleep
- grep –ignore-case –only
- Multi-line string with extra space (preserved indentation)
- What are .a and .so files?
- How to delete from a text file, all lines that contain a specific string?
- Multi-line string with extra space (preserved indentation)
- How do I grep recursively?
- ‘\r’: command not found – .bashrc / .bash_profile [duplicate]
- How to mkdir only if a directory does not already exist?