Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

escaping quotes and double quotes in bash alias

153 views
Skip to first unread message

alb

unread,
Jan 8, 2017, 9:58:17 AM1/8/17
to
Hi everyone,

I've found an interesting oneliner that would post process apt-get output and
show info on to-be-updated packages:

apt-get --just-print upgrade 2>&1 | perl -ne 'if
(/Inst\s([\w,\-,\d,\.,~,:,\+]+)\s\[([\w,\-,\d,\.,~,:,\+]+)\]\s\(([\w,\-,\d,\.,~,:,\+]+)\)?
/i) {print "PROGRAM: $1 INSTALLED: $2 AVAILABLE: $3\n"}' | column -t

(credits:
http://unix.stackexchange.com/questions/19470/list-available-updates-but-do-not-install-them)

so I thought it would have been interesting to make an alias out of it, but I'm
having hard time to escape quotes and double quotes.

Any ideas?

Al

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Kenny McCormack

unread,
Jan 8, 2017, 10:12:27 AM1/8/17
to
In article <edf2c5...@mid.individual.net>, alb <a...@notmymail.com> wrote:
>Hi everyone,
>
>I've found an interesting oneliner that would post process apt-get output and
>show info on to-be-updated packages:
...
>having hard time to escape quotes and double quotes.
>
>Any ideas?

The quick answer is: Do it as a function, not as an alias. The bash
documentation is pretty clear that aliases are pretty much TBA (which of
course begs the question as to why they were ever included...), and that
anything you can do with an alias, you can do with a function, but the
converse does not hold. In particular, and unlike in (t)csh, you cannot
pass arguments to a bash alias (except, of course, at the end).

The less-quick answer is: It can be done (as long as you don't need to pass
"internal" arguments), but it gets ugly and weird. Having grown up on tcsh
(and still preferring it to bash, although I end up using bash when I'm in
an environment that doesn't have tcsh installed), I, perversely, still like
aliases and have devoted time (way too much time, in fact) to building ways
to handle the ugly quoting problems. I won't go into it here (at least not
now), since it is so ugly (and, ultimately, pointless), but it is/was fun
putting it together.

--
Debating creationists on the topic of evolution is rather like trying to
play chess with a pigeon --- it knocks the pieces over, craps on the
board, and flies back to its flock to claim victory.

Allodoxaphobia

unread,
Jan 8, 2017, 12:59:18 PM1/8/17
to
On 8 Jan 2017 14:58:13 GMT, alb wrote:
> Hi everyone,
>
> I've found an interesting oneliner that would post process apt-get output and
> show info on to-be-updated packages:
>
> apt-get --just-print upgrade 2>&1 | perl -ne 'if
> (/Inst\s([\w,\-,\d,\.,~,:,\+]+)\s\[([\w,\-,\d,\.,~,:,\+]+)\]\s\(([\w,\-,\d,\.,~,:,\+]+)\)?
> /i) {print "PROGRAM: $1 INSTALLED: $2 AVAILABLE: $3\n"}' | column -t
>
> (credits:
> http://unix.stackexchange.com/questions/19470/list-available-updates-but-do-not-install-them)
>
> so I thought it would have been interesting to make an alias out of it, but I'm
> having hard time to escape quotes and double quotes.
>
> Any ideas?

What purpose is served by pounding all that into an alias?
What does that do that ~/bin/one-liner.sh can't do?

Jonesy -- just curious...
--
Marvin L Jones | Marvin | W3DHJ.net | linux
38.238N 104.547W | @ jonz.net | Jonesy | FreeBSD
* Killfiling google & XXXXbanter.com: jonz.net/ng.htm

alb

unread,
Jan 8, 2017, 1:58:39 PM1/8/17
to
Hi Jonesy,

Allodoxaphobia <knock_you...@example.net> wrote:
> On 8 Jan 2017 14:58:13 GMT, alb wrote:
>> Hi everyone,
>>
>> I've found an interesting oneliner that would post process apt-get output and
>> show info on to-be-updated packages:
>>
[]
>
> What purpose is served by pounding all that into an alias?
> What does that do that ~/bin/one-liner.sh can't do?

Not much more indeed, except the fact that I find it more convenient to maintain
it in a .bashrc file rather than carrying scripts around. Except for the
interest in knowing how to escape quotes I admit that it doesn't do much more.

Al

alb

unread,
Jan 8, 2017, 2:08:38 PM1/8/17
to
Hi Kenny,

Kenny McCormack <gaz...@shell.xmission.com> wrote:
> In article <edf2c5...@mid.individual.net>, alb <a...@notmymail.com> wrote:
>>Hi everyone,
>>
>>I've found an interesting oneliner that would post process apt-get output and
>>show info on to-be-updated packages:
> ...
>>having hard time to escape quotes and double quotes.
[]
> The quick answer is: Do it as a function, not as an alias.
[]

Ok, I'll dig into it before posting some nonesense, I'm not that proficient with
bash and I'll need to look up some examples before putting something together.

> The less-quick answer is: It can be done (as long as you don't need to pass
> "internal" arguments), but it gets ugly and weird. Having grown up on tcsh
> (and still preferring it to bash, although I end up using bash when I'm in
> an environment that doesn't have tcsh installed), I, perversely, still like
> aliases and have devoted time (way too much time, in fact) to building ways
> to handle the ugly quoting problems.

I'm working with tcsh at work due to the fact that CAD tools are vastly
configured through tcsh by our IT department (environment configuration is done
through tcsh and it would be useless to port it to bash), but on my local
machine I use bash since I grew up (a little) on that and I'm not to the point
where I can compare the two. Maybe one day...

Al

Kenny McCormack

unread,
Jan 8, 2017, 3:12:39 PM1/8/17
to
In article <slrno74vfh.5ka.k...@vps.jonz.net>,
Allodoxaphobia <If_you_mus...@my.sig.adr> wrote:
...
>What purpose is served by pounding all that into an alias?
>What does that do that ~/bin/one-liner.sh can't do?

There are advantages to writing one-liner type things as either aliases or
(in bash) functions rather than making each one a separate executable
script.

--
People who say they'll vote for someone else because Obama couldn't fix
*all* of Bush's messes are like people complaining that he couldn't cure
cancer, so they'll go and vote for (more) cancer.

Rakesh Sharma

unread,
Jan 9, 2017, 6:30:24 AM1/9/17
to
On Sunday, 8 January 2017 20:28:17 UTC+5:30, alb wrote:

>
> apt-get --just-print upgrade 2>&1 | perl -ne 'if
> (/Inst\s([\w,\-,\d,\.,~,:,\+]+)\s\[([\w,\-,\d,\.,~,:,\+]+)\]\s\(([\w,\-,\d,\.,~,:,\+]+)\)?
> /i) {print "PROGRAM: $1 INSTALLED: $2 AVAILABLE: $3\n"}' | column -t
>
> so I thought it would have been interesting to make an alias out of it, but I'm
> having hard time to escape quotes and double quotes.
>
> Any ideas?
>

alias ax='apt-get --just-print upgrade 2>&1 | perl -ne '\''if
(/Inst\s([\w,\-,\d,\.,~,:,\+]+)\s\[([\w,\-,\d,\.,~,:,\+]+)\]\s\(([\w,\-,\d,\.,~,:,\+]+)\)?
/i) {print "PROGRAM: $1 INSTALLED: $2 AVAILABLE: $3\n"}'\'' | column -t'

change all unescaped single quotes ' ---> '\''

Helmut Waitzmann

unread,
Jan 9, 2017, 7:04:20 PM1/9/17
to
alb <a...@notmymail.com>:
> Hi Kenny,
>
> Kenny McCormack <gaz...@shell.xmission.com> wrote:
>> In article <edf2c5...@mid.individual.net>, alb <a...@notmymail.com> wrote:
>>>Hi everyone,
>>>
>>>I've found an interesting oneliner that would post process apt-get output and
>>>show info on to-be-updated packages:
>> ...
>>>having hard time to escape quotes and double quotes.
> []
>> The quick answer is: Do it as a function, not as an alias.
> []
>
> I'm not that proficient with bash and I'll need to look up some
> examples before putting something together.

Just put the oneliner into a shell function, e.g.:

my_apt-get-upgradables()
{

alb

unread,
Jan 12, 2017, 5:46:04 PM1/12/17
to
Helmut Waitzmann <nn.th...@xoxy.net> wrote:
[]
>> I'm not that proficient with bash and I'll need to look up some
>> examples before putting something together.
>
> Just put the oneliner into a shell function, e.g.:
>
> my_apt-get-upgradables()
> {
> apt-get --just-print upgrade 2>&1 | perl -ne 'if
> (/Inst\s([\w,\-,\d,\.,~,:,\+]+)\s\[([\w,\-,\d,\.,~,:,\+]+)\]\s\(([\w,\-,\d,\.,~,:,\+]+)\)?
> /i) {print "PROGRAM: $1 INSTALLED: $2 AVAILABLE: $3\n"}' |
> column -t
> }

Thanks for the hint!

Al
0 new messages