How to execute a bash script?

Andrew

So I've written my first bash script:

#!/bin/bash
echo 'hello world!'
exit

I know it has the right location to bash and is executable:

$ which bash
/bin/bash
$ chmod +x myscript.sh

Now I want to run it from the command line, but I get an error:

$ myscript.sh
myscript.sh: command not found

So instead I try this and it works:

$ bash myscript.sh
hello world!

Is this how I will always need to execute it? I feel like I have executed other scripts without having to precede it with bash. How can I run myscript.sh without having to precede it with bash?

Update: Here is a good explanation of why and how to execute a bash script.

pupeno

You have to make the file executable. You can do that with

chmod +x <filename>

where is the name of your script and then you have to prepend it with ./ to instruct the shell to run a file in the local directory, like:

./script.sh

You can only run files that are in your PATH or that you specify a path to them. ./, the local directory, is not in the PATH by default because someone may use it for nefarious purposes. Imagine a script called ls dropped in a directory, you go inside that directory, run ls and that script does something bad.

While you are at it you may want to make it more portable by running shell instead of bash by using:

#!/bin/sh

or by running bash no matter where it is installed as long as it is installed:

#!/usr/bin/env bash

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to execute git commands from bash script?

From Dev

How to execute a bash script as sbt task?

From Dev

How to execute SQL command with parameters in bash script

From Dev

How to execute custom command in bash script

From Dev

How to execute a Bash script from Github?

From Dev

How to execute a command within a bash script?

From Dev

How to execute recurring Bash script at specific times?

From Dev

how to execute a bash command in a python script

From Dev

How to execute the following commands in a bash script?

From Dev

How to execute bash script on a remote machine asynchronously

From Dev

How to execute bash (in script) with own .bash_logout file?

From Dev

Go: execute bash script

From Dev

Bash Script - Will not completely execute

From Dev

Bash script failing to execute bash script

From Dev

How do I write a dockerfile to execute a simple bash script?

From Dev

Bash script: How to execute commands consecutively without waiting for the previous one?

From Dev

How can force a bash script to execute in www, and not executing in filesystem/*?

From Dev

How to execute commands in docker container as part of bash shell script

From Dev

Bash script: How to execute commands consecutively without waiting for the previous one?

From Dev

How can force a bash script to execute in www, and not executing in filesystem/*?

From Dev

How to execute sudo commands with Expect & send commands in bash script?

From Dev

How to execute shell commands in a loop from within a bash script?

From Dev

how to execute program which is in my environment variable in a bash script

From Dev

how to execute multiple lines of code inside SSH in bash script

From Dev

How do I write a dockerfile to execute a simple bash script?

From Dev

how to execute a bash script when someone connects to the ssh server (freebsd)

From Dev

How does running this bash script as sudo execute contents as root

From Dev

How to execute a bash script before shutdown using systemd?

From Dev

how to find bash script, then execute it and pass some arguments

Related Related

  1. 1

    How to execute git commands from bash script?

  2. 2

    How to execute a bash script as sbt task?

  3. 3

    How to execute SQL command with parameters in bash script

  4. 4

    How to execute custom command in bash script

  5. 5

    How to execute a Bash script from Github?

  6. 6

    How to execute a command within a bash script?

  7. 7

    How to execute recurring Bash script at specific times?

  8. 8

    how to execute a bash command in a python script

  9. 9

    How to execute the following commands in a bash script?

  10. 10

    How to execute bash script on a remote machine asynchronously

  11. 11

    How to execute bash (in script) with own .bash_logout file?

  12. 12

    Go: execute bash script

  13. 13

    Bash Script - Will not completely execute

  14. 14

    Bash script failing to execute bash script

  15. 15

    How do I write a dockerfile to execute a simple bash script?

  16. 16

    Bash script: How to execute commands consecutively without waiting for the previous one?

  17. 17

    How can force a bash script to execute in www, and not executing in filesystem/*?

  18. 18

    How to execute commands in docker container as part of bash shell script

  19. 19

    Bash script: How to execute commands consecutively without waiting for the previous one?

  20. 20

    How can force a bash script to execute in www, and not executing in filesystem/*?

  21. 21

    How to execute sudo commands with Expect & send commands in bash script?

  22. 22

    How to execute shell commands in a loop from within a bash script?

  23. 23

    how to execute program which is in my environment variable in a bash script

  24. 24

    how to execute multiple lines of code inside SSH in bash script

  25. 25

    How do I write a dockerfile to execute a simple bash script?

  26. 26

    how to execute a bash script when someone connects to the ssh server (freebsd)

  27. 27

    How does running this bash script as sudo execute contents as root

  28. 28

    How to execute a bash script before shutdown using systemd?

  29. 29

    how to find bash script, then execute it and pass some arguments

HotTag

Archive