I’d call it a bad practice but the initial stages of bioinformatic analysis often take form of a CLI trial and error. Where one modifies and reruns long oneliners until one finally works. While this approach feels responsive it makes piecing together a proper script difficult, especially when one works ona server with multiple people accessing the same user (which polutes ~/.bash_history).

With heredocs it’s possible to create a bash script that creates it’s own rcfile, for eg. to set up a bash session with a separate history file and predefined functions.

Examples

Hello World!

Bash script that creates a Hello World! program in python and runs it. trap 'rm script.py' EXIT deletes the python script after receiving the EXIT signal.

#!/usr/bin/env bash

VAR='Hello World!'

cat > script.py << EOF
val = "$VAR"
print(val)
EOF

python script.py

trap 'rm script.py' EXIT
## Hello World!

Passing arrays

Bash script that declares a (2 3 4 5 6 7) array, creates a python script that parses a bash array into a list and prints it.

#!/usr/bin/env bash

echo "We're in Bash now."

readarray -t arr < <(seq 2 7)

# print the array as a string and print its length
echo "${arr[*]}"
echo "${#arr[@]}"

cat > script.py << EOF
print("Were in python now.")

# parse bash array into a python list
arr = "${arr[*]}".split(' ')
print(f"{arr}\n{len(arr)}")
EOF

echo ===
echo "This is the python script."
cat script.py

echo ===
echo "This is the python output."

python script.py
echo ===

trap 'rm script.py' EXIT
## We're in Bash now.
## 2 3 4 5 6 7
## 6
## ===
## This is the python script.
## print("Were in python now.")
## 
## # parse bash array into a python list
## arr = "2 3 4 5 6 7".split(' ')
## print(f"{arr}\n{len(arr)}")
## ===
## This is the python output.
## Were in python now.
## ['2', '3', '4', '5', '6', '7']
## 6
## ===

Bio-Informatics Shell Logger

A prototype of what I had in mind. This script creates a hidden .hist directory if it doesn’t exist, and stores the session HISTFILE in it. HISTFILE name is todays date with an additional random number between 0 and 99 as a hack to prevent overwriting histfiles created in the same directory on the. same day.

A custom PS1 prompt let’s you know that you’re running bash with dynamically generated rcfile.

#!/usr/bin/env bash

start_info() {
    cat << EOF >&2
Welcome to BISL, the Bio-Informatics Shell Logger.

get_help to get help.

Ctrl+D or exit to quit.
EOF
}

[[ ! -d .hist ]] && mkdir .hist/
FILENAME="$(pwd)/.hist/$(date '+%y%m%d')$(( RANDOM % 100 )).bislhist"

cat > .tmprc << EOF
HISTCONTROL='ignoredups'
HISTFILE=${FILENAME}
HISTIGNORE="ls*:cd*"
HISTSIZE=-1

PS1=":-=[BISL SESSION]=-:\n[\u@\h \W]\n└─$ "

clear
start_info
EOF

trap 'rm .tmprc' EXIT

bash --rcfile .tmprc -i

© 2025 MHryc, released under the GNU GPL v3.0.