Home > Software engineering >  Bash script: any way to collect remainder of command line as a string, including quote characters?
Bash script: any way to collect remainder of command line as a string, including quote characters?

Time:08-22

The following simplified version of a script I'll call logit obviously just appends everything but $1 in a text file, so I can keep track of time like this:

$ logit Started work on default theme

But bash expansion gets confused by quotes of any kind. What I'd like is to do things like

$ logit Don't forget a dark mode

But when that happens of course shell expansion rules cause a burp:

quote>

I know this works:

# Yeah yeah I can enclose it in quotes but I'd prefer not to
$ logit "Don't forget a dark mode"

Is there any way to somehow collect the remainder of the command line before bash gets to it, without having to use quotes around my command line?

Here's a minimal working version of the script.

#!/bin/bash
log_file=~/log.txt
now=$(date  "%T %r")
echo "${now} ${@:1}" >>  $log_file

CodePudding user response:

Is there any way to somehow collect the remainder of the command line before bash gets to it, without having to use quotes around my command line?

No. There is no "before bash gets into it" time. Bash reads the input you are typing, Bash parses the input you are typing, there is nothing in between or "before". There is only Bash.

You can: use a different shell or write your own. Note that quotes parsing like in shell is very common, you may consider that it could be better for you to understand and get used to it.

CodePudding user response:

you can use a backslash "\" before the single quote

 $ logit Don\'t forget a dark mode
  •  Tags:  
  • bash
  • Related