Bash (Unix shell): Difference between revisions

From David's Wiki
Line 33: Line 33:
* <code>-gt</code> is <code>></code>
* <code>-gt</code> is <code>></code>
* <code>-ge</code> is <code>>=</code>
* <code>-ge</code> is <code>>=</code>
===Check if File/Folder Exists===
See [https://linuxize.com/post/bash-check-if-file-exists/#check-if-directory-exist https://linuxize.com/post/bash-check-if-file-exists/#check-if-directory-exist].


==<code>.bashrc</code>==
==<code>.bashrc</code>==

Revision as of 06:44, 7 June 2020

Bash Scripting


Getting Started

Here is an example bash script

#!/bin/bash
# A simple variable example
myvariable=Hello
anothervar=Fred

echo $myvariable $anothervar

You can check your bash scripts using https://www.shellcheck.net/.

Usage

Basic If Statements

https://linuxize.com/post/bash-if-else-statement/

if [[ $VAR -gt 10 ]] then
  echo "The variable is greater than 10."
elif [[ $VAR -eq 10 ]] then
  echo "The variable is equal to 10."
else
  echo "The variable is less than 10."
fi

Comparisons

  • -eq is ==
  • -gt is >
  • -ge is >=

Check if File/Folder Exists

See https://linuxize.com/post/bash-check-if-file-exists/#check-if-directory-exist.

.bashrc

Your .bashrc file will be loaded for each terminal.

Presentation of Shell Variable

Add the following to show your

PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$'