Bash script template
Description
This script create a starter bash script with a main function and set the execute bit flag.
Source
#!/usr/bin/env bash
# Create hello world bash script from template and set x flag
main() {
local newfile="${1:?Specify file}"
[[ -f "${newfile}" ]] && {
echo "File already exist"
exit 1
}
cat <<-EOF >"${newfile}"
#!/usr/bin/env bash
main() {
echo hello world
}
main "\$@"
EOF
chmod +x "${newfile}"
echo "${newfile}"
}
main "$@"Instructions
- Create a new file with your favorite editor
- Paste the source and save it as
touchx - Set the executable flag:
chmod +x touchx
Usage
To create and edit a new script in the same command
$EDITOR "$(touchx mynewscript)"
Note: The EDITOR environment variable specifies the default text editor used by command-line programs. If not set consult your shell manual or replace $EDITOR with the name of your favorite editor.
Links
$HOME