Git Product home page Git Product logo

ha.sh's Introduction

Multi-line commands in Bash

Shell commands in Unix-like systems often take the following format:

command argument0 --option1 argument1 --option2 argument2

Sometimes, when applicable options and arguments are numerous or lengthy, it is helpful to break a command into multiple lines, using backslashes:

command argument0 \
    --option1 argument1 \
    --option2 argument2

Limitations in Bash syntax

Bash is unable to correctly interpret a multi-line command if comments are placed at the end of any line except the last, or between the lines:

command argument0 \       # this comment breaks the command
    --option1 argument1 \ # this one too
    --option2 argument2   # only this is okay

command argument0 \
    # this breaks things
    --option1 argument1 \
    # so does this
    --option2 argument2

Doing the above causes the comments and subsequent lines to be interpreted as separate commands, contrary to the programmer's intention.

Relevant post on Stack Overflow: How to put a line comment for a multi-line command

Relevant post on Unix & Linux Stack Exchange: How to comment multi-line commands in shell scripts?

Having to append backslashes to every line also seems like an avoidable chore.

The value of comments in multi-line commands

Many programs have one-letter options that are easy to type but difficult to read:

du -chs *     # what do options c, h and s do?
du -c -h -s * # equivalent to above

Comments would help readers understand the purpose of the command and options without having to read the man page:

du     # estimate file space usage
    -c # produce a grand total
    -h # print sizes in human-readable format
    -s # display only a total for each argument
    *

Note: backslashes have been omitted, see section below

Comments provide not only helpful information but also a way to toggle/disable certain options without having to delete lines:

command argument0
    #--option1 argument1
    --option2 argument2

Proposing an alternative to backslashes

Bash assumes that every line is a separate command, unless the preceding line is terminated by a backslash.

An alternative to this approach is to assume that every line is a continuation of the line preceding it, unless the preceeding line is empty.

Augmenting Bash syntax

ha.sh gives the ability to execute shell scripts with the following syntax:

command         # look Ma, no backslashes!
    #--switch   # use comments to turn switches on and off
    -f          # explain non-descriptive options

# remember to leave an empty line between commands

command argument0
    # comments can appear between lines of a command
    --option1 argument1
    # as long as the lines are contiguous
    --option2 argument2

For the shell, in the shell

ha.sh is itself a Bash script. Besides Bash builtins, it uses only echo and sed, core utilities that are shipped with most Unix-like systems.

Installation and Usage

Inspect ha.sh and give it a try!

Download ha.sh to /usr/local/bin:

sudo curl https://raw.githubusercontent.com/joel-ling/ha.sh/master/ha.sh \
    -o /usr/local/bin/ha.sh 

Make it executable:

sudo chmod 755 /usr/local/bin/ha.sh

Check that /usr/local/bin is in $PATH:

echo $PATH | grep /usr/local/bin

Execute a shell script written in ha.sh syntax:

ha.sh /tmp/example.ha.sh

Translate to vanilla Bash without executing:

ha.sh -t /tmp/example.ha.sh
ha.sh -t /tmp/example.ha.sh > /tmp/example.sh

Uninstalling ha.sh is as easy as:

rm -v /usr/local/bin/ha.sh

ha.sh's People

Stargazers

SiangHwa avatar

Watchers

Joel Ling avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.