Programming/Bash: Difference between revisions

From Dev Wiki
Jump to navigation Jump to search
(Document shebang line)
(Document for loops)
Line 55: Line 55:
=== File and Folder Checks ===
=== File and Folder Checks ===
For a full list of built-in args to check file and folder status, see https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions
For a full list of built-in args to check file and folder status, see https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions
== For Loops ==
for <loop_variable> in $<my_iterable_object>
do
    # Logic to execute on each loop.
done
For example:
for item in $my_list
do
    echo $item
done


== String Manipulation ==
== String Manipulation ==

Revision as of 08:43, 7 October 2020

Bash is primarily a Linux scripting language, but it works on all versions of Windows as well, if used through git.

File Start

Unless you have explicit reason for otherwise, essentially all bash files can safely start with the line:

#!/usr/bin/env bash

It essentially tells your script what interpreter to use when executing the file.

Comments

Inline Comments

# This is an inline comment.

Block Comments

Block level comments don't truly exist in Bash.

However, there is a hackish way to implement them anyways, according to https://stackoverflow.com/a/43158193 { warn | It's recommended to use multiple inline comments instead, as this may not always work with all systems. }

: '
This is a block comment.
Comment line 2.
Another block comment line.
'

Variables

Variable Definition

a_bool=true
b_bool=false
my_var_1="This is "
my_var_2="a string."

Variable Usage

echo "Printing variable values."
echo $a_bool
echo $b_bool
echo ${my_var_1}${my_var_2}

If Statements

Basic If

if [[ $x == $y ]]
then
    # Logic if true.
fi

Full If

if [[ $x == $y ]]
then
    # Logic for "if" true.
elif [[ $x && ($y || $z) ]]
then
    # Logic for "else if" true.
else
    # Logic for false.
fi

File and Folder Checks

For a full list of built-in args to check file and folder status, see https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions


For Loops

for <loop_variable> in $<my_iterable_object>
do
    # Logic to execute on each loop.
done

For example:

for item in $my_list
do
    echo $item
done


String Manipulation

See https://stackoverflow.com/a/14703709

With bash, it's possible to dynamically trim strings, based on regex matches.
The syntax is:

# Trim shortest match from beginning.
${<string_value>#<regex>}

# Trim longest match from beginning.
${<string_value>##<regex>}

# Trim shortest match from end.
${<string_value>%<regex>}

# Trim longest match from end.
${<string_value>%%<regex>}


For example, if you have a string of

file_name="/home/user/my_dir/my_dir/my_file.tar.gz"

Then you can do the following manipulations:

# Get the full file extension.
# Outputs "tar.gz"
${file_name#*.}

# Get the last part of the file extension.
# Outputs "gz"
${file_name##*.}

# Get the full file name, including file extension.
# Outputs "my_file.tar.gz"
echo "b: ${file_name##*/}

# Get parent of current directory.
# Outputs "/home/user/my_dir/"
${file_name%my_dir/*}

# Get grandparent of current directory.
# Outputs "/home/user/"
${file_name%%my_dir/*}