# How do I do \<insert thing here\> in bash?

**URL:** <https://ask.cyberinfrastructure.org/t/how-do-i-do-insert-thing-here-in-bash/744>\
**Category:** Q&A\
**Tags:** hpc-getting-started, bash, researcher\
**Created:** [January 11, 2019, 7:11pm UTC](https://ask.cyberinfrastructure.org/t/how-do-i-do-insert-thing-here-in-bash/744 "2019-01-11T19:11:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vsoch](https://ask.cyberinfrastructure.org/user_avatar/ask.cyberinfrastructure.org/vsoch/32/112_2.png) [@vsoch](https://ask.cyberinfrastructure.org/u/vsoch)\
**Post date:** [January 11, 2019, 7:11pm UTC](https://ask.cyberinfrastructure.org/t/how-do-i-do-insert-thing-here-in-bash/744/1 "2019-01-11T19:11:49Z")

</div>

In high performance computing, we generally work in a terminal, meaning using some shell. Have you ever seen these lines at the top of your “shell scripts?”

```bash
#!/bin/sh

```

or

```bash
#!/bin/sh

```

Those first lines are the interpreter lines - or an instruction for what program to use to run the script. If you look at the variable `SHELL` in your terminal, you likely will see the shell that _you are using as we speak!_

```bash
echo $SHELL
/bin/bash

```

There is a [rich history](https://en.wikipedia.org/wiki/Bash_(Unix_shell)) of how the Unix shell became the “Bourne again” shell, or what we commonly refer to as bash.

> So why bash?

The reason I want you to get excited about bash is because there are SO many things you can do with it that you would typically rely on a higher level language (python, R, etc.) to do. For example, you can trim strings, write functions to otherwise manipulate strings, calculate quantities, read files, count things… it’s a powerful language! Here is a one line function to make a string all lowercase:

```bash
lower() {
    printf '%s\n' "${1,,}"
}

$ lower LiKeOmGTaCoS
likeomgtacos

```

Specifically, I want to share [THE BASH BIBLE](https://github.com/dylanaraps/pure-bash-bible) that is not only interesting, it’s fun and a great resource to learn from! So the next time you want to do some special thing from the command line? Ask yourself if you can do it with bash first, before you delve into requiring a higher level language dependency.

---

<div class="post-metadata">

**Author:** ![mhanby](https://ask.cyberinfrastructure.org/user_avatar/ask.cyberinfrastructure.org/mhanby/32/50_2.png) [@mhanby](https://ask.cyberinfrastructure.org/u/mhanby)\
**Post date:** [January 15, 2019, 10:00pm UTC](https://ask.cyberinfrastructure.org/t/how-do-i-do-insert-thing-here-in-bash/744/2 "2019-01-15T22:00:19Z")

</div>

It’s also very simple to use loops in BASH:

- Simple **for** loop

```bash
for node in compute{1..10}; do
  ssh $node 'hostname -s'
done

```

- Simple **while** loop

```bash
count=1
while /bin/true; do
  echo "looped ${count} times"
  let count=${count}+1
  sleep 10
done

```

And classic **if then else** statement

```bash
result=0

if [["$result" == "0"]];then
  echo "Success"
else
  echo "Fail"
fi

```

---

<div class="post-metadata">

**Author:** ![vsoch](https://ask.cyberinfrastructure.org/user_avatar/ask.cyberinfrastructure.org/vsoch/32/112_2.png) [@vsoch](https://ask.cyberinfrastructure.org/u/vsoch)\
**Post date:** [January 15, 2019, 10:22pm UTC](https://ask.cyberinfrastructure.org/t/how-do-i-do-insert-thing-here-in-bash/744/3 "2019-01-15T22:22:17Z")

</div>

Agreed! Another useful example is to loop over a listing of files, or generally any terminal command that produces a list of things:

```bash
for file in $(ls $PWD); 
    do 
    echo $file; 
done

```

That would generate an echo of all the files in the present working directory. You can imagine running a command instead and using the variable! Another simple loop is literally just a line of space separated strings, like this:

```bash
$ for thing in "one" "two";
    do 
        echo $thing 
    done

```

That would print out:

```bash
one
two

```

![image](https://ask.cyberinfrastructure.org/uploads/default/original/1X/c9082f89fe6207c900c02e9342612ef92814ad90.jpeg)

---

<div class="post-metadata">

**Author:** ![mhanby](https://ask.cyberinfrastructure.org/user_avatar/ask.cyberinfrastructure.org/mhanby/32/50_2.png) [@mhanby](https://ask.cyberinfrastructure.org/u/mhanby)\
**Post date:** [June 3, 2020, 4:00pm UTC](https://ask.cyberinfrastructure.org/t/how-do-i-do-insert-thing-here-in-bash/744/4 "2020-06-03T16:00:09Z")

</div>

Here’s another one that came from [nixCraft](https://www.facebook.com/nixcraft/) that I use all the time. Add this to your `~/.bashrc` file to create a simple to use calculator:

```auto
calc() { echo "scale=2; $*" | bc -l; }
calc_full() { echo "$*" | bc -l; }

```

These lines create 2 functions:

- `calc`: Results are trimmed to 2 decimal places
- `calc_full`: Results are not trimmed

The usage is simple:

```auto
$ calc "1024*8" # Convert KB into MB
8192

$ calc "40*1.8+32" # Convert Celcius to Fahrenheit
104.0

$ calc "(2+2)*(2-2)" # You can also control the order of operation with ( )
0

```
