What's the difference between: . [script] or source [script], bash [script] or $SHELL [script], and ./ [script] or [script]?
. script
and source script
execute the contents of script
in the current environment, i.e. without creating a subshell. On the upside this allows script
to affect the current environment, for example changing environment variables or changing the current work directory. On the downside this allows script
to affect the current environment, which is a potential security hazard.
bash script
passes script
to the bash
interpreter to execute. Whatever shebang is given by script
itself is ignored. ("Shebang" referring to the first line of script
, which could e.g. read #!/bin/bash
, or #!/usr/bin/perl
, or #!/usr/bin/awk
, to specify the interpreter to be used.)
$SHELL script
passes script
to whatever is your current shell interpreter to execute. That may, or may not, be bash
. (The environment variable SHELL
holds the name of your current shell interpreter. $SHELL
, if running bash, is evaluated to /bin/bash
, with the effect detailed in the previous paragraph.)
./script
executes the contents of a file script
in the current work directory. If there is no such file, an error is generated. The contents of $PATH
have no effect on what happens.
script
looks for a file script
in the directories listed in $PATH
, which may or may not include the current work directory. The first script
found in this list of directories is executed, which may or may not be the one in your current work directory.
Using dot or source while calling another script - what is the difference?
There is no difference.
From the manual:
source
source filename
A synonym for . (see Bourne Shell Builtins).
source command not found in sh shell
/bin/sh
is usually some other shell trying to mimic The Shell. Many distributions use /bin/bash
for sh
, it supports source
. On Ubuntu, though, /bin/dash
is used which does not support source
. Most shells use .
instead of source
. If you cannot edit the script, try to change the shell which runs it.
No such file or directory but it exists
This error can mean that ./arm-mingw32ce-g++
doesn't exist (but it does), or that it exists and is a dynamically linked executable recognized by the kernel but whose dynamic loader is not available. You can see what dynamic loader is required by running ldd /arm-mingw32ce-g++
; anything marked not found
is the dynamic loader or a library that you need to install.
If you're trying to run a 32-bit binary on an amd64 installation:
- Up to Ubuntu 11.04, install the package
ia32-libs
. - On Ubuntu 11.10, install
ia32-libs-multiarch
. - Starting with 12.04, install
ia32-libs-multiarch
, or select a reasonable set of:i386
packages in addition to the:amd64
packages.
How to check if an environment variable exists and get its value?
[ -z "${DEPLOY_ENV}" ]
checks whether DEPLOY_ENV
has length equal to zero. So you could run:
if [[ -z "${DEPLOY_ENV}" ]]; then
MY_SCRIPT_VARIABLE="Some default value because DEPLOY_ENV is undefined"
else
MY_SCRIPT_VARIABLE="${DEPLOY_ENV}"
fi
# or using a short-hand version
[[ -z "${DEPLOY_ENV}" ]] && MyVar='default' || MyVar="${DEPLOY_ENV}"
# or even shorter use
MyVar="${DEPLOY_ENV:-default_value}"
Read a variable in bash with a default value
You can use parameter expansion, e.g.
read -p "Enter your name [Richard]: " name
name=${name:-Richard}
echo $name
Including the default value in the prompt between brackets is a fairly common convention
What does the :-Richard
part do? From the bash manual:
${parameter:-word}
If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
Also worth noting that...
In each of the cases below, word is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.
So if you use webpath=${webpath:-~/httpdocs}
you will get a result of /home/user/expanded/path/httpdocs
not ~/httpdocs
, etc.
Difference between source and ./ execution of linux scripts
check if variable "a" is set in your current shell:
set | grep '^a='
Remember that once you source script to your current shell,
all it's global variables are there until unset or you exit the current shell.
You may want to start a new shell, source the script, end exit shell to perform valid tests.
I don't know the context of your problem, but you may want to export some key variables to have their copies in every subprocess.
Related Topics
Redirect All Output to File Using Bash on Linux
Convert All File Extensions to Lower-Case
Shell Script to Get the Process Id on Linux
What Is a Sysroot Exactly and How to Create One
Adding a Directory to Path in Ubuntu
Execute Shell Commands from Program Running in Wine
Which Drivers Are Used by Usb Mouse in Linux Kernel
How to Tell Whether I'm in a Screen
Linux's Thread Local Storage Implementation
Which Signal Does Ctrl-X Send When Used in a Terminal
Postgresql -Bash: Psql: Command Not Found
Do (Statically Linked) Dlls Use a Different Heap Than the Main Program
Sort Across Multiple Files in Linux
How to Correctly Install Wkhtmltopdf on Debian 64 Bit
What Does Double-Dash Do When Following a Command
Linux Shell Script for Each File in a Directory Grab the Filename and Execute a Program