[LINUX] [Ubuntu] How to execute a shell script

$bash ./test.sh
$sh ./test.sh
$ ./test.sh

pattern 1

The contents of the script

$ cat test.sh
echo "test!";

Execution (success)

$ sh test.sh
test!

Pattern 2

The contents of the script

$ cat test.sh
#!/bin/bash

array=(1 2 3);
echo "test!";

L3 has an array notation that can only be used in bash.

Execution (failure)

$ sh test.sh
test.sh: 3: test.sh: Syntax error: "(" unexpected

I put #! / Bin / bash at the beginning of the script, but for some reason I get angry because bash is not working.

Execution (failure) 2

Try putting ./ at the beginning of the file name

$ sh ./test.sh 
./test.sh: 3: ./test.sh: Syntax error: "(" unexpected

I still get angry.

Execution (success)

$ ./test.sh 
test!

You can do this by specifying only the file without using sh, or by using bash.

$ bash test.sh
test!

By the way

$ test.sh 
test.sh: command not found

If you don't add ./, you will be angry because it seems to be a command.


Postscript

I added the bash command with reference to @ hidezzz's comment. Thank you for your professor!

Recommended Posts

[Ubuntu] How to execute a shell script
How to run a Python program from within a shell script
I'll never forget how to write a shell script, don't forget! !!
Execute a script from Jupyter to process
Creating a shell script to write a diary
How to run a Maya Python script
How to determine if a shell script was started in bash
Verification of how to periodically execute a script on a Linux server on Windows
How to call a function
Let's try a shell script
How to hack a terminal
How to create a simple TCP server / client script
How to execute a command using subprocess in Python
How to use Fujifilm X-T3 as a webcam on Ubuntu 20.04
[Ubuntu] How to delete the entire contents of a directory
How to build a new python virtual environment on Ubuntu
[Linux] Copy data from Linux to Windows with a shell script
How to execute external shell scripts and commands in python
How to write a Python class
How to make a slack bot
How to create a Conda package
How to make a crawler --Advanced
How to make a recursive function
How to create a virtual bridge
How to install MBDyn (Linux Ubuntu)
How to make a deadman's switch
How to create a Dockerfile (basic)
[Blender] How to make a Blender plugin
How to delete a Docker container
How to make a crawler --Basic
How to install Go on Ubuntu
How to create a config file
How to pass arguments to a Python script in SPSS Modeler Batch
Conditional branch due to the existence of a shell script file
Create a shell script to run the python file multiple times
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
Shell script to build pyenv environment on ubuntu in one shot
How to automatically execute python script (with GUI) and sudo permission command after logging in with Ubuntu 18.04
[Blender] How to set shape_key with script
How to create a clone from Github
How to split and save a DataFrame
How to build a sphinx translation environment
How to create a git clone folder
Qiita (1) How to write a code name
How to add a package with PyCharm
[Python] How to make a class iterable
How to draw a graph using Matplotlib
[Python] How to convert a 2D list to a 1D list
How to use OpenVPN with Ubuntu 18.04.3 LTS
[maya] How to operate / execute maya's userSetup.py
[Colab] How to copy a huge dataset
[Python] How to invert a character string
How to install a package using a repository
How to execute commands in jupyter notebook
A simple IDAPython script to name a function
How to get a stacktrace in python
[Cocos2d-x] How to make Script Binding (Part 2)
I made a script to display emoji
Manage logrotate generations with a shell script.
How to install php7.4 on Linux (Ubuntu)
How to create a repository from media