Here is a simple example showing that using declare
in a script the script will not run, while sourcing the script will:
$ cat /tmp/new
#! /bin/sh
declare -i hello
$ chmod a+rwx /tmp/new
$ /tmp/new
/tmp/new: 3: declare: not found
$ source /tmp/new
$
I wonder why directly running the script doesn't work, while sourcing it does? How can I make the first one work? Thanks!
Best Answer
declare
is a builtin function and it's not available with/bin/sh
, only withbash
orzsh
(and maybe other shells). The syntax may differ from one shell to another. You must choose your sheebang (#!
) accordingly: if the script needs to be run by bash, the first line must beor