In Bash scripting, is there a single statement alternative for this?
if [ -f /path/to/some/file ]; then source /path/to/some/file fi
The most important thing is that the filename is there only once, without making it a variable (which adds even more lines).
For example, in PHP you could do it like this
@include("/path/to/some/file"); // @ makes it ignore errors
Check if Directory Exist The operators -d allows you to test whether a file is a directory or not. [ -d /etc/docker ] && echo "$FILE is a directory." You can also use the double brackets [[ instead of a single one [ .
A file is sourced in two ways. One is either writting as source <fileName> or other is writting as . ./<filename> in the command line. When a file is sourced, the code lines are executed as if they were printed on the command line.
The source command reads and executes commands from the file specified as its argument in the current shell environment. It is useful to load functions, variables, and configuration files into shell scripts. source is a shell built-in in Bash and other popular shells used in Linux and UNIX operating systems.
Is defining your own version of @include
an option?
include () { [[ -f "$1" ]] && source "$1" } include FILE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With