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 execute a program or call a system command?
- How do I grep recursively?
- How do I iterate over a range of numbers defined by variables in Bash?
- Connect: Socket operation on non-socket
- How to set ssh timeout?
- subprocess.Popen(): OSError: [Errno 8] Exec format error in python?
- Using find to locate files that match one of multiple patterns
- How can I recursively find all files in current and subfolders based on wildcard matching?
- Curl command for https ( SSL )
- What is the purpose of “&&” in a shell command?
- How to generate random number in Bash?
- How do I split a string on a delimiter in Bash?
- adb shell su works but adb root does not
- What does WEXITSTATUS(status) return?
- What is a list in Bash?
- Open and write data to text file using Bash?
- adb shell su works but adb root does not
- How to remove the quotes when reading a variable in jq in shell?
- cp: missing destination file operand after
- Pseudo-terminal will not be allocated because stdin is not a terminal
- python getoutput() equivalent in subprocess
- screen Cannot open your terminal ‘/dev/pts/0’ – please check
- Is there a TRY CATCH command in Bash
- How to convert a string to lower case in Bash?
- dquote> result of a execution a program in linux shell
- Using sudo with Python script
- * failed to open vchiq instance
- Copy multiple files from one directory to another from Linux shell
- How to use sed to extract substring
- Restarting cron after changing crontab file?
- Shell Script: Execute a python program from within a shell script
- How do I change my private key passphrase?
- How do I get the current Unix time in milliseconds in Bash?
- Is there a way to do a remote “ls” much like “scp” does a remote copy?