DEV Community

Kir Axanov
Kir Axanov

Posted on

Code. Bash arguments: last and all previous

TL;DR

In bash we can get the last argument like this:

${@: -1}
Enter fullscreen mode Exit fullscreen mode

And all the previous arguments like this:

${@:1:$#-1}
Enter fullscreen mode Exit fullscreen mode

General

You need to call some arbitrary program from a script, but you don't know how many arguments will it have.
You know that this program does something on a file.
So our script will receive at least two arguments: a command (to call our program) and a file name.
(Yes, we can change the arguments order for file to be first, but I think this is not so ideomatic.)

Here is a simple bash-script for that:

#!/usr/bin/env bash
CMD="${@:1:$#-1}"
FILE="${@: -1}"

if [[ -z "$CMD" ]]
then
  echo "No open command."
  exit 1
fi

if [[ -z "$FILE" ]]
then
  echo "No file to open."
  exit 1
fi

$CMD "$FILE"
Enter fullscreen mode Exit fullscreen mode

Call it like so: open.sh COMMAND FILE.

Broot

Originally, I was looking for a way to open files from broot file manager. By design, broot pauses while the file is opened - so it is not particularly comfortable (I mean, possible) to open several files at once.
But we can instruct broot to open files with an arbitrary script, in which we can run target command in a background (see nohup and disown at the bottom).

Here is a slightly modified script for broot to call. Place it as ~/.config/broot/open_detached.sh:

#!/usr/bin/env bash
# Open files and leave broot working.
# Source: https://github.com/Canop/broot/issues/38#issuecomment-583843143
CMD="${@:1:$#-1}"
FILE="${@: -1}"

if [[ -z "$CMD" ]]
then
  echo "No open command."
  exit 1
fi

if [[ -z "$FILE" ]]
then
  echo "No file to open."
  exit 1
fi

nohup $CMD "$FILE" > /dev/null & disown
Enter fullscreen mode Exit fullscreen mode

Now we can use that script in broot config. Here is an example how I configured image opening with feh:

verbs: [
  {
    key: enter
    extensions: [
      "xcf" "xbm" "xpm" "ico" "svg"
      "jpeg" "jpg" "bmp" "png" "tiff"
    ]
    execution: "~/.config/broot/open_detached.sh feh --scale-down --auto-zoom {file}"
    leave_broot: false
  }
]
Enter fullscreen mode Exit fullscreen mode

Shout-out to oj-lappi for inspiration!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay