What is the difference between running a script file like this:
sh myfile.sh
Or this:
./myfile.sh
I thought they are the same thing, but noticed sometimes they are not, as seen below example:
root@kali:~/Documents# sh sweep-ips.sh
ips range:
ping: 10.11.1.{0..24}: Name or service not known
root@kali:~/Documents# ./sweep-ips.sh
ips range:
64 bytes from 10.11.1.5: icmp_seq=1 ttl=128 time=242 ms
64 bytes from 10.11.1.8: icmp_seq=1 ttl=64 time=249 ms
64 bytes from 10.11.1.22: icmp_seq=1 ttl=255 time=249 ms
64 bytes from 10.11.1.24: icmp_seq=1 ttl=64 time=241 ms
script:
#!/bin/bash
echo "ips range"
for ip in 10.11.1.{0..24}
do
ping -c 1 ${ip} | grep "time="
done
$shell ./script.sh
run it through$shell
, but./script.sh
runs it through whatever the#!
line in the script says. In this case I suppose you have the shebang (quite correctly!) set to Bash or such but that yoursh
is just POSIX sh (likedash
on Debian and Ubuntu). – ilkkachu Dec 05 '17 at 18:47./mycmd
, since in that case you don't need to think what the expected interpreter is. (Instead of a shell script, it could be Perl, orawk
, orexpect
, or...) The only reason to usesh ./mycmd
orbash ./mycmd
would be to test how the script runs with that particular shell, perhaps to test for portability (or lack of it). – ilkkachu Dec 05 '17 at 19:01