Bash
Online Bash Shell
Your script runs with bash main.sh on GNU Bash 5.2, in a Linux sandbox with the usual command-line tools and no network. read waits for you to type in the terminal while the script runs.
Try it aboveHow Bash runs here
- Versions
- GNU Bash 5.2
- Run
bash main.sh- Limits per run
- 256 MB memory, 15 s of CPU time on half a core, 2 MB of output
- Time
- Up to 10 minutes a run (30 on a paid plan); waits 2 minutes for input (5 on a paid plan)
- Sandbox
- Each run gets its own Linux sandbox with no network access. Text files your program writes next to your code open as new tabs.
A Bash script that reads input
This main.sh reads numbers into an array, sorts them with sort and paste, and uses awk for the one thing Bash arithmetic can’t do, decimals:
#!/bin/bash printf 'How many numbers? ' read -r n nums=() for ((i = 1; i <= n; i++)); do printf 'Number %d: ' "$i" read -r x nums+=("$x") done sum=0 for x in "${nums[@]}"; do (( sum += x )); done sorted=$(printf '%s\n' "${nums[@]}" | sort -n | paste -sd ' ') average=$(awk -v s="$sum" -v n="$n" 'BEGIN { printf "%.2f", s / n }') echo "Sorted: $sorted" echo "Sum: $sum" echo "Average: $average (Bash alone would say $(( sum / n )))"
Four numbers typed in, one per prompt:
$ bash main.sh How many numbers? 4 Number 1: 12 Number 2: 7 Number 3: 30 Number 4: 5 Sorted: 5 7 12 30 Sum: 54 Average: 13.50 (Bash alone would say 13)
Reading input in Bash here
- read -p "Name: " name prints nothing here. Bash shows the -p prompt only when input comes from a terminal device, and a script’s input here doesn’t. Print the prompt yourself: printf 'Name: '; read -r name.
- Use read -r so backslashes in the input stay as typed. read -r a b rest splits a line at spaces: with 1 2 3 4, a is 1, b is 2 and rest is "3 4".
- while read -r line loops until the end of input, and this terminal never sends one (there’s no Ctrl+D). Read a count first, as above, or stop on an empty line, as below.
- Program arguments from Run settings arrive as $1, $2 and "$@". They are split at spaces and quotes don’t group words, so "hello world" becomes two arguments.
declare -A count echo "Type words, one per line. An empty line stops." while read -r word && [[ -n $word ]]; do (( count[${word,,}]++ )) done for w in "${!count[@]}"; do printf '%s %d\n' "$w" "${count[$w]}" done | sort
Commands you can use
The sandbox is a Debian system without network access. These are there:
- Text tools: grep, sed, awk (mawk), sort, uniq, cut, tr, paste, wc, head, tail, tee, diff.
- Files: ls, cp, mv, rm, mkdir, find, xargs, tar, gzip. Your script runs in the folder that holds your files.
- Other: date, seq, sleep, ps, and sqlite3 for SQL on a database file or in memory.
Several scripts work together: source ./lib.sh to load functions from lib.sh, or run another script with bash other.sh. Text files your script writes next to main.sh open as new tabs after the run.
What isn’t available here
- Network tools and access: curl, wget, ssh, git and ping aren’t installed, and nothing can connect out.
- jq, bc, python and node. Use awk for decimal math, and grep or sed for simple JSON when you know its shape.
- sudo and apt-get. You can’t install packages.
- An interactive shell. The playground runs your script from start to finish; a terminal you type commands into, kept running in a project, is a Premium feature.
Errors you’ll see
- “main.sh: line 1: total: command not found” from total = 5. Assignments take no spaces around =: total=5.
- “main.sh: line 2: [: abc: integer expression expected” -gt and -lt compare whole numbers, and the variable held text. Check the input first, or compare text with = inside [[ ]].
- “main.sh: line 1: 2.5 + 1 : syntax error: invalid arithmetic operator (error token is ".5 + 1 ")” $(( )) works on integers only. Use awk for decimals.
- “main.sh: line 2: count: unbound variable” With set -u, a variable that was never set stops the script. Set a default: ${count:-0}.
- “main.sh: line 3: bc: command not found” bc isn’t installed (nor are jq and curl). Use awk "BEGIN { print 7 / 3 }".
- “main.sh: line 2: unexpected EOF while looking for matching `}'” An unclosed ${...}, quote or brace. The line number is where Bash gave up, so look above it.
Bash 5.2 in practice
- Case changes are built in: ${name^^} gives upper case, ${name,,} lower case.
- Associative arrays (declare -A) count and group without awk, as in the word counter above.
- $EPOCHSECONDS gives the current Unix time without calling date.
- set -euo pipefail at the top stops on the first failing command, unset variable or failed pipe stage, which makes errors show up where they happen.
- Use [[ ]] for tests: it handles empty variables and supports && and pattern matching like [[ $file == *.txt ]].
Questions
Which shell runs my script?
GNU Bash 5.2, as bash main.sh. The #!/bin/bash line is fine to keep, but the script runs with bash either way.
Is this an interactive Linux terminal?
No. It runs your script from start to finish and lets it read input while it runs. You can’t type commands at a prompt.
Why doesn’t read -p show my prompt?
Bash prints the -p prompt only when input comes from a terminal. Print it with printf 'Prompt: ' and then call read -r.
Are awk, sed and grep available?
Yes, along with sort, cut, tr, find, tar and sqlite3. curl, wget, jq and bc are not.
How do I pass arguments to a Bash script online?
Type them in Program arguments in Run settings and read them as $1, $2 and "$@". They are split at spaces.