Skip to content

Latest commit

 

History

History
99 lines (75 loc) · 2.7 KB

File metadata and controls

99 lines (75 loc) · 2.7 KB

Hello Bash

A cross-platform shell. Double quotes always recommended for cross-platform compatibility. See my new-machine repo for installation and my recommended .bashrc file. This How-To Geek article on the difference between terminal, shell, and console also covers TTY (teletypewriter).

  • Bash reference manual
    • "[Bash] is intended to be a conformant implementation of the IEEE POSIX Shell and Tools portion of the IEEE POSIX specification (IEEE Standard 1003.1)."
    • --posix aligns more closely with POSIX. However, I don't use this flag.
    • "For almost every purpose, shell functions are preferable to aliases."

windows/.bashrc

I don't use Windows enough to try to generalize my AI-generated "create symlink" script there, so just copy paste that file where you need it!

link-bashrc.sh and .bashrc

Note that this .bashrc file is specific to Linux Mint and may not behave well on other operating systems.

chmod +x ./link-bashrc.sh
./link-bashrc.sh
source ./.bashrc

Notes

Variables

# Print "Hello world, x is 2" to stdout (excluding quotes)
# No spaces allowed between var name, `=`, and var value
x=2
# Spaces allowed within quoted strings.
# Quotes are not part of var value.
y="Hello world"
echo $y, x is $x

Aliases

# Open the .bashrc file in VS Code
alias bashedit='code ~/.bashrc'

However, functions are preferable to aliases according to the reference manual.

Functions

This function uses jq to edit JSON files, editing the local package.json by default:

json_edit() {
    if [ $# -eq 0 ]; then
        echo "Usage: json_edit 'jq_filter' [file]"
        echo "Examples:"
        echo "  json_edit '.author = \"Mark Wiemer\"'"
        echo "  json_edit '.scripts.build = \"tsc\"'"
        echo "  json_edit 'del(.devDependencies.eslint)'"
        return 1
    fi

    local filter="$1"
    local file="${2:-package.json}"

    jq "$filter" "$file" > temp.json && mv temp.json "$file"
    echo "Applied filter: $filter"
}

These functions create or reset the package.json file, with some customizations just for me:

new_project() {
    pnpm init
    pnpm add -D sort-package-json
    json_edit '.author="Mark Wiemer"'
    json_edit '.scripts."validate:fix"="sort-package-json"'
    local dir_name=$(basename "$PWD")
    json_edit ".name=\"@mark-wiemer/$dir_name\""
    pnpm run validate:fix
}

reset_project() {
    rm package.json
    new_project
}