LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-07-2020, 09:11 PM   #16
nicedreams
Member
 
Registered: Jun 2003
Location: Phoenix, AZ
Posts: 112

Original Poster
Rep: Reputation: 17

I put the function (with quote issue, but not a major limitation) on github if anyone wants to use it.

https://github.com/nicedreams/bashmarks
 
Old 08-08-2020, 01:57 AM   #17
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,768

Rep: Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192
Indeed the shell strips \escape and "quotes" and 'ticks' from command arguments.
And a function is like a command.
Example
Code:
echo "hello world"
The echo command does not get the quotes so cannot process=print them.

You might want to look further for a pre-cmd feature in bash...
 
Old 08-08-2020, 04:58 AM   #18
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,882

Rep: Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988
My suggestion would be to pass the command via stdin rather than trying to pass it using function argument(s) (which'll never work, for reasons people have already commented on).

Code:
$ cat example.sh 
#!/bin/bash

CMDFILE=/tmp/commands.txt

store_command()
{
  cat >> "$CMDFILE"
}


store_command <<-"EOD"
  asillycommand -w "with quotes and such" -l 'woah! quotes!' *.input
EOD

$ ./example.sh 
$ cat commands.txt 
  asillycommand -w "with quotes and such" -l 'woah! quotes!' *.input
$
Quoted "Here Documents" (as used above) pass their contents as literals with no quote removal, path expansion, or variable substitution applied.

Another option would be to base64 encode the string before you pass it as an argument and have the function decode it on the other side. But that's getting a little involved and using stdin is easier.


Personally though, for what it sounds like you're wanting to do, I'd just set a bunch of shell command aliases for frequently run commands instead of writing a front-end script like this, or maybe even leverage shell history functionality.
 
Old 08-08-2020, 10:14 AM   #19
nicedreams
Member
 
Registered: Jun 2003
Location: Phoenix, AZ
Posts: 112

Original Poster
Rep: Reputation: 17
Quote:
Personally though, for what it sounds like you're wanting to do, I'd just set a bunch of shell command aliases for frequently run commands instead of writing a front-end script like this, or maybe even leverage shell history functionality.
I was just screwing around with a notes function I found that was a bunch of if statements and figured I could improve it since was bored. Then had idea about recalling a line in the .note file as a command. Then had some fun trying to solve this issue.
 
Old 08-09-2020, 02:53 AM   #20
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by nicedreams View Post
I'm going to say this is something that cannot be done in bash.
It's very simple really:

If you are reading the command line arguments in from an interactive shell you can not preserve double quotes because they're eliminated byt he shell before the whole thing even gets to your script.
This is not such a big problem however; the shell knows to preserve functionality. You could read in the arguments, and "file with spaces" would still become 1 member of the argument list, not 3.
(There's still the "${@@Q}" option - not exactly what you want because it simply wraps everything in single quotes, but it might prove useful nevertheless)

What you want is to read command line arguments into a script in a way that these command line arguments become verbatim rsync command line arguments, right?
Have you considered either
  • reconstructing them, maybe after reading them in with getopt(s), or
  • reading them in in a different way, e.g. with a read command
?

Once these arguments have properly entered your script it's all very easy IMO:
Code:
read -r -a arg
echo "${arg[@]}"
echo "${arg[@]}" > somefile
cat somefile
 
  


Reply

Tags
quoting, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Difference between echo of file within double quotes and without double quotes ankitpandey Programming 2 01-11-2013 09:02 AM
Yet Another Bash Quotes Within Quotes Issue tboyer Linux - Software 17 11-03-2012 11:17 AM
Double Quotes Inside Double Quotes youarefunny Programming 6 06-09-2010 10:21 PM
Problems with quotes and double quotes Andruha Slackware 6 01-02-2010 04:44 PM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 11:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration