27. Creating Your First Script (The Shebang and Execution)
Let's create and execute a simple script that greets the user and reports the current directory.
Step 1: Create the Script File
Use nano to create a file called welcome.sh.
bash $ nano welcome.sh
Content of welcome.sh:
bash #!/bin/bash
Script to welcome the user
echo "---------------------------------" echo "Welcome to Termux, user: $(whoami)" echo "Today is: $(date)" echo "Current location: $(pwd)" echo "---------------------------------"
Step 2: Grant Execution Permission
Without execute permission, the shell cannot run the file directly.
bash $ chmod +x welcome.sh
(The +x flag is shorthand for adding execute permission for all users, equivalent to chmod 755 if it was already 644).
Step 3: Running the Script
To run a script located in your current directory, you must specify the path, typically ./ (current directory).
bash $ ./welcome.sh
Welcome to Termux, user: u0_a200 Today is: Tue Jun 1 10:30:00 UTC 2024 Current location: /data/data/com.termux/files/home
If you forget chmod +x, Termux will return a Permission denied error.