Bash: Difference between revisions

Jump to navigation Jump to search
397 bytes added ,  14 March 2022
Wooo syntax highlighting is awesome
imported>Sjwhitak
(Mediawiki's links are made with square brackets and that messes up the if statements)
imported>Sjwhitak
(Wooo syntax highlighting is awesome)
 
Variables are super simple, here is a basic example of a hello world program bash file.
 
<syntaxhighlight lang="bash">
#!/bin/bash
#!/bin/bash
 
greeting="You are a warm popsicle"
greeting="You are a warm popsicle"
echo $greeting
echo $greeting
</syntaxhighlight>
 
When creating and assigning a value to a variable, make sure to include no spaces between the variable name, the equal sign, and the value of the variable. Then when you want to call a variable for use, like the greeting, we use a dollar sign to indicate that it is a variable. Note, variables like $1, $0, $11 are reserved for command line arguments so avoid using them.
=== Input ===
Read values inline:
<syntaxhighlight lang="bash">
#!/bin/bash
 
echo "Guess a number"
#!/bin/bash
# Use read to create the number variable and
# immediately set the variable to the input
echo "Guess a number"
# Use read to create the number variable and
echo "You get slapped $number times."
# immediately set the variable to the input
</syntaxhighlight>
read number
echo "You get slapped $number times."
 
Read parameters from command-line:
<syntaxhighlight lang="bash">
#!/bin/bash
 
# Prints the name of the file
#!/bin/bash
echo $0
# Prints the namefirst ofparameter passed to the file
echo $01
# Prints the first parameter passed to the file
echo $1
# $@ is used to represent all of the command line inputs
# Here we are just iterating through all command line inputs
for param in "$@"
do
echo $param
done
 
# $@ is used to represent all of the command line inputs
# Here we are just iterating through all command line inputs
for param in "$@"
do
echo $param
done
</syntaxhighlight>
 
=== Operators ===
 
As a side note, when comparing two strings, always put strings or variables referencing strings in parenthesis and compare using == or !=.
syntaxhighlight lang="bash">
 
if [ "$foo" == "$bar"]
# or
if [ "$foo" != "$bar"]
</syntaxhighlight>
 
 
=== If, Elif, Else ===
<syntaxhighlight lang="bash">
<pre>
#!/bin/bash
 
echo "You can't get anything right you hopeless loser"
fi
</syntaxhighlight>
</pre>
 
This is a simple example that just reads in a number and checks to see if the number is either 1, 2, or, if neither of these are true, will just insult you. The syntax here is pretty simple. You have the if statement which uses square brackets for the conditional and elif statements which use double square brackets. There is also the default else statement and the whole statement is closed off by a fi (if backwards).
 
For:
<syntaxhighlight lang="bash">
#!/bin/bash
 
# Iterates through all objects in an array, all inputsguessed, etc.
#!/bin/bash
for word in $paragraph
do
# Iterates through all objects in an array, all inputsguessed, etc.
echo $word
for word in $paragraph
done
do
</syntaxhighlight>
echo $word
done
 
While:
<syntaxhighlight lang="bash">
#!/bin/bash
 
# Runs while the conditional is being met
#!/bin/bash
while [ $index -lt 5 ]
do
# Runs while the conditional is being met
echo $index
while [ $index -lt 5 ]
# Incrementing values is obnoxious
do
echo index=$((index + 1))
done
# Incrementing values is obnoxious
</syntaxhighlight>
index=$((index + 1))
done
 
Until:
<syntaxhighlight lang="bash">
#!/bin/bash
 
# Runs until the conditional is met
#!/bin/bash
until [ $index -eq 5 ]
do
# Runs until the conditional is met
echo $index
until [ $index -eq 5 ]
# Still obnoxious
do
echo index=$((index + 1))
done
# Still obnoxious
</syntaxhighlight>
index=$((index + 1))
done
 
=== Iterating through files in a directory ===
<syntaxhighlight lang="bash">
#!/bin/bash
 
# Path to the directory we want to check out
#!/bin/bash
files=~/Downloads/*
# Path to the directory we want to check out
# The for loop is back baby
files=~/Downloads/*
for file in $files
do
# The for loop is back baby
echo $file
for file in $files
# Prints out the absolute path of each file
do
done
echo $file
</syntaxhighlight>
# Prints out the absolute path of each file
done
 
Bash is able to automatically detect whether a variable represents a directory or not. It's magic and is definitely not regex.
 
=== Aliases ===
You can use aliases to create cli commands from bash files. Aliases can be set in your ~/.bashrc file. Make sure to make your bash file executable and move it to your
<syntaxhighlight lang="bash">
 
alias epiccommand='./epiccommand.sh'
alias epiccommand='./epiccommand.sh "Nerds"'
</syntaxhighlight>
Anonymous user

Navigation menu