Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #255578 > unrolled thread

alias in bash script issue

Started byKen Young <ken@highwinds.cloud>
First post2023-03-04 09:40 +0100
Last post2023-03-06 11:10 +0100
Articles 20 — 9 participants

Back to article view | Back to linux.debian.user


Contents

  alias in bash script issue Ken Young <ken@highwinds.cloud> - 2023-03-04 09:40 +0100
    Re: alias in bash script issue David <bouncingcats@gmail.com> - 2023-03-04 11:00 +0100
      Re: alias in bash script issue Tom Furie <tom@furie.org.uk> - 2023-03-04 14:20 +0100
        Re: alias in bash script issue David <bouncingcats@gmail.com> - 2023-03-04 15:50 +0100
          Re: alias in bash script issue Greg Wooledge <greg@wooledge.org> - 2023-03-04 17:30 +0100
      Re: alias in bash script issue David Wright <deblis@lionunicorn.co.uk> - 2023-03-04 18:20 +0100
        Re: alias in bash script issue Greg Wooledge <greg@wooledge.org> - 2023-03-04 18:30 +0100
          Re: alias in bash script issue David Wright <deblis@lionunicorn.co.uk> - 2023-03-05 21:40 +0100
            Re: alias in bash script issue Greg Wooledge <greg@wooledge.org> - 2023-03-05 22:40 +0100
              Re: alias in bash script issue David Wright <deblis@lionunicorn.co.uk> - 2023-03-07 03:50 +0100
      Re: alias in bash script issue Ken Young <ken@highwinds.cloud> - 2023-03-05 02:30 +0100
        Re: alias in bash script issue Greg Wooledge <greg@wooledge.org> - 2023-03-05 02:40 +0100
          Re: alias in bash script issue Tom Browder <tom.browder@gmail.com> - 2023-03-05 12:30 +0100
            Re: alias in bash script issue Ken Young <ken@highwinds.cloud> - 2023-03-05 12:40 +0100
              Re: alias in bash script issue Tom Browder <tom.browder@gmail.com> - 2023-03-05 12:50 +0100
                Re: alias in bash script issue Nicolas George <george@nsup.org> - 2023-03-05 14:30 +0100
                  Re: alias in bash script issue <tomas@tuxteam.de> - 2023-03-05 17:10 +0100
                  Re: alias in bash script issue Tom Browder <tom.browder@gmail.com> - 2023-03-07 13:10 +0100
              Re: alias in bash script issue <tomas@tuxteam.de> - 2023-03-05 14:10 +0100
    Re: alias in bash script issue Yassine Chaouche <a.chaouche@algerian-radio.dz> - 2023-03-06 11:10 +0100

#255578 — alias in bash script issue

FromKen Young <ken@highwinds.cloud>
Date2023-03-04 09:40 +0100
Subjectalias in bash script issue
Message-ID<G5vAR-9YKb-3@gated-at.bofh.it>

[Multipart message — attachments visible in raw view] — view raw

Hello,

Do you know why my alias can't work in the bash script?
The info is as follows.

1)  this alias does exist

$ alias |grep 'k='

alias k='minikube kubectl --'


2) it also exists in .bash_profile

$ cat ~/.bash_profile |grep 'k='

alias k="minikube kubectl --"


3) the content of bash script

$ cat get.sh

#!/bin/bash


source ~/.bash_profile

k get node


4) alias can't work

$ ./get.sh

./get.sh: line 5: k: command not found


The system is debian 11 and the shell is bash.

Please give suggestions. Thank you.


regards.

[toc] | [next] | [standalone]


#255584

FromDavid <bouncingcats@gmail.com>
Date2023-03-04 11:00 +0100
Message-ID<G5wQh-9ZrY-1@gated-at.bofh.it>
In reply to#255578
On Sat, 4 Mar 2023 at 19:30, Ken Young <ken@highwinds.cloud> wrote:

> Do you know why my alias can't work in the bash script?
> The info is as follows.
>
> 1)  this alias does exist
> $ alias |grep 'k='
> alias k='minikube kubectl --'
>
> 2) it also exists in .bash_profile
> $ cat ~/.bash_profile |grep 'k='
> alias k="minikube kubectl --"

[...]

> ./get.sh: line 5: k: command not found

[...]

> Please give suggestions.

Hi. Friends don't help friends to do bad things :)

Debian's default shell is 'dash'. Its manual, readable using 'man dash',
says

    Aliases provide a convenient way for naive users to create shorthands
    for commands without having to learn how to create functions with
    arguments.  They can also be used to create lexically obscure code.
    This use is discouraged

To achieve your goal, do this instead.

In .bash_profile, replace the line
  alias k='minikube kubectl --'

with these two lines:
  k() { minikube kubectl "$@" ; }
  export k

[toc] | [prev] | [next] | [standalone]


#255585

FromTom Furie <tom@furie.org.uk>
Date2023-03-04 14:20 +0100
Message-ID<G5zXP-a1ze-3@gated-at.bofh.it>
In reply to#255584

[Multipart message — attachments visible in raw view] — view raw

On Sat, Mar 04, 2023 at 08:52:15PM +1100, David wrote:
> Debian's default shell is 'dash'. Its manual, readable using 'man dash',
> says

The script explicitly calls bash. The rest of your point is still
(coincidentally) valid though, as it would be in most of the popular shells
as far as I'm aware.

Cheers,
Tom

-- 
To understand the heart and mind of a person, look not at what
he has already achieved, but at what he aspires to do.

[toc] | [prev] | [next] | [standalone]


#255586

FromDavid <bouncingcats@gmail.com>
Date2023-03-04 15:50 +0100
Message-ID<G5BmV-a2k3-7@gated-at.bofh.it>
In reply to#255585
On Sun, 5 Mar 2023 at 00:12, Tom Furie <tom@furie.org.uk> wrote:
> On Sat, Mar 04, 2023 at 08:52:15PM +1100, David wrote:

> > Debian's default shell is 'dash'. Its manual, readable using 'man dash',
> > says
>
> The script explicitly calls bash. The rest of your point is still
> (coincidentally) valid though, as it would be in most of the popular shells
> as far as I'm aware.

I am aware. 'man dash' better describes how aliases should be
used in modern times, so I referred to it to reference an authority
greater than my own personal opinion.

'man bash' avoids this clear language, which is unfortunate
because that advice is what anyone who knows what they are
doing will advise.

So, my point was made intentionally, not coincidentally.
Thanks for drawing attention to that ambiguity in my message.

[toc] | [prev] | [next] | [standalone]


#255587

FromGreg Wooledge <greg@wooledge.org>
Date2023-03-04 17:30 +0100
Message-ID<G5CVH-a3pI-21@gated-at.bofh.it>
In reply to#255586
On Sun, Mar 05, 2023 at 01:39:35AM +1100, David wrote:
> I am aware. 'man dash' better describes how aliases should be
> used in modern times,

???

Is this the paragraph you mean?

     Aliases provide a convenient way for naive users to create shorthands for
     commands without having to learn how to create functions with arguments.
     They can also be used to create lexically obscure code.  This use is dis‐
     couraged.

> 'man bash' avoids this clear language, which is unfortunate
> because that advice is what anyone who knows what they are
> doing will advise.

Do you mean this:

       For almost every purpose, aliases are superseded by shell functions.

Your statements are so incredibly vague that I can't tell what you're
trying to say.

In any case, the Subject: header still says "bash script", and in bash
scripts, aliases are disabled by default.  They have to be enabled
explicitly (shopt -s expand_aliases), before they can even be used.

[toc] | [prev] | [next] | [standalone]


#255590

FromDavid Wright <deblis@lionunicorn.co.uk>
Date2023-03-04 18:20 +0100
Message-ID<G5DI5-a3Xs-5@gated-at.bofh.it>
In reply to#255584
On Sat 04 Mar 2023 at 20:52:15 (+1100), David wrote:

> Hi. Friends don't help friends to do bad things :)
> 
> Debian's default shell is 'dash'. Its manual, readable using 'man dash',
> says
> 
>     Aliases provide a convenient way for naive users to create shorthands
>     for commands without having to learn how to create functions with
>     arguments.

IMHO manpages should be about function, not policy. I don't like
this condescension. man bash   is more straightforward:

  For almost every purpose, aliases are superseded by shell functions.

> They can also be used to create lexically obscure code.

What has that got to do with the price of fish?

In view of the Obfuscated Perl Contest, is the author railing
against writing anything in Perl?

>     This use is discouraged

There's a place for warning about the use of aliases in, say,
Greg's BashPitfalls, or books, but not here.

> To achieve your goal, do this instead.
> 
> In .bash_profile, replace the line
>   alias k='minikube kubectl --'
> 
> with these two lines:
>   k() { minikube kubectl "$@" ; }
>   export k

Overkill for those I've written. And I would discourage the use
of that sort of shortcut function anywhere else but interactively.
Exporting it and then using it in scripts is going to produce
code that's just as lexically obscure.

I guess it falls to me to provide some examples of mine:

  alias 1234='printf AmexD\\n'

Redacted; one of several. (I don't have an American Express card.)

  alias cal='ncal -3 -A1'

For me, cal output needs transposing. This range is so useful.

  alias cdeject='eject # mask cdeject which produces I/O error and locks up drive'

From the days when I played red-book CDs at work.

  alias hekspace='od -t a -t x1 -An'

Can anyone memorise all those switches.

  alias py='python3'

A sophisticated calculator.

  alias tnef='tnef -v --directory ~/Documents --number-backups'

Curse MS Exchange's attachments.

Finally, I'm not arguing against manpage warnings like this:

  --drq-hsm-error
    VERY DANGEROUS, DON'T EVEN THINK ABOUT USING IT. …
    … VERY DANGEROUS, DO NOT USE!! 

  (man hdparm)

that guard against accidental bricking.

Cheers,
David.

[toc] | [prev] | [next] | [standalone]


#255592

FromGreg Wooledge <greg@wooledge.org>
Date2023-03-04 18:30 +0100
Message-ID<G5DRL-a41h-5@gated-at.bofh.it>
In reply to#255590
On Sat, Mar 04, 2023 at 11:14:39AM -0600, David Wright wrote:
> >     This use is discouraged
> 
> There's a place for warning about the use of aliases in, say,
> Greg's BashPitfalls, or books, but not here.

Discouraging the use of legacy features that are considered dangerous
in modern usage is fair game for software manuals, I would say.

[toc] | [prev] | [next] | [standalone]


#255638

FromDavid Wright <deblis@lionunicorn.co.uk>
Date2023-03-05 21:40 +0100
Message-ID<G63jb-amPC-7@gated-at.bofh.it>
In reply to#255592
On Sat 04 Mar 2023 at 12:23:58 (-0500), Greg Wooledge wrote:
> On Sat, Mar 04, 2023 at 11:14:39AM -0600, David Wright wrote:
> > >     This use is discouraged
> > 
> > There's a place for warning about the use of aliases in, say,
> > Greg's BashPitfalls, or books, but not here.
> 
> Discouraging the use of legacy features that are considered dangerous
> in modern usage is fair game for software manuals, I would say.

But it doesn't point out the dangers, dangers that you can
easily duplicate with functions. What I objected to is the tone,
a tone that was picked up in the first line of the first reply.

There are many lists of dangerous aliases on the web, but I haven't
found a list of such aliases that can't be transcribed into functions.

(I'm not sure where modern usage comes in.)

Cheers,
David.

[toc] | [prev] | [next] | [standalone]


#255642

FromGreg Wooledge <greg@wooledge.org>
Date2023-03-05 22:40 +0100
Message-ID<G64ff-anAR-5@gated-at.bofh.it>
In reply to#255638
On Sun, Mar 05, 2023 at 02:37:48PM -0600, David Wright wrote:
> There are many lists of dangerous aliases on the web, but I haven't
> found a list of such aliases that can't be transcribed into functions.

Here's one document:

https://www.chiark.greenend.org.uk/~sgtatham/aliases.html

[toc] | [prev] | [next] | [standalone]


#255664

FromDavid Wright <deblis@lionunicorn.co.uk>
Date2023-03-07 03:50 +0100
Message-ID<G6vyN-aFLo-1@gated-at.bofh.it>
In reply to#255642
On Sun 05 Mar 2023 at 16:30:10 (-0500), Greg Wooledge wrote:
> On Sun, Mar 05, 2023 at 02:37:48PM -0600, David Wright wrote:
> > There are many lists of dangerous aliases on the web, but I haven't
> > found a list of such aliases that can't be transcribed into functions.
> 
> Here's one document:
> 
> https://www.chiark.greenend.org.uk/~sgtatham/aliases.html

An enjoyable article, thanks. A nice illustration of
Cambridge computer scientists' ingenuity.

But it clearly has no connection with my criticism of man dash.
So, you can combine a carefully crafted pairing of alias and
function (a "magic alias") to achieve an effect that's not
possible using either one or the other. That's not the naive
user who hasn't learned how to create functions with arguments.
Nor are the contructions /lexically/ obscure; the trap is
rather in the evaluation's occurring in an unexpected context.
Just how dangerous that will be is moot.

My impression is that the most dangerous frequently used
aliases are those that redefine commands perceived as unsafe;
a prime example being rm → rm -i.

Cheers,
David.

[toc] | [prev] | [next] | [standalone]


#255599

FromKen Young <ken@highwinds.cloud>
Date2023-03-05 02:30 +0100
Message-ID<G5Lmi-aafG-1@gated-at.bofh.it>
In reply to#255584

[Multipart message — attachments visible in raw view] — view raw

On Sat, Mar 4, 2023 at 5:53 PM David <bouncingcats@gmail.com> wrote:

> On Sat, 4 Mar 2023 at 19:30, Ken Young <ken@highwinds.cloud> wrote:
>
> > Do you know why my alias can't work in the bash script?
> > The info is as follows.
> >
> > 1)  this alias does exist
> > $ alias |grep 'k='
> > alias k='minikube kubectl --'
> >
> > 2) it also exists in .bash_profile
> > $ cat ~/.bash_profile |grep 'k='
> > alias k="minikube kubectl --"
>
> [...]
>
> > ./get.sh: line 5: k: command not found
>
> [...]
>
> > Please give suggestions.
>
> Hi. Friends don't help friends to do bad things :)
>
> Debian's default shell is 'dash'. Its manual, readable using 'man dash',
> says
>
>
>


Hi,

my debian 11's default shell is just bash.

root@nxacloud-bloghost:~# echo $SHELL

/bin/bash

root@nxacloud-bloghost:~# lsb_release -cd

Description: Debian GNU/Linux 11 (bullseye)
Codename: bullseye

Thanks

[toc] | [prev] | [next] | [standalone]


#255600

FromGreg Wooledge <greg@wooledge.org>
Date2023-03-05 02:40 +0100
Message-ID<G5LvX-aaiS-1@gated-at.bofh.it>
In reply to#255599
On Sun, Mar 05, 2023 at 09:26:41AM +0800, Ken Young wrote:
> On Sat, Mar 4, 2023 at 5:53 PM David <bouncingcats@gmail.com> wrote:
> > Debian's default shell is 'dash'. Its manual, readable using 'man dash',
> > says

> my debian 11's default shell is just bash.
> 
> root@nxacloud-bloghost:~# echo $SHELL
> 
> /bin/bash

Both statements are correct, within their contexts.

The default *interactive* shell for user accounts is /bin/bash.

The default /bin/sh symlink (for scripts using #!/bin/sh, etc.) is dash.

The author of a script chooses whether the shebang says #!/bin/bash
(which makes it a bash script) or #!/bin/sh (which makes it an sh script).
The languages are different.

[toc] | [prev] | [next] | [standalone]


#255618

FromTom Browder <tom.browder@gmail.com>
Date2023-03-05 12:30 +0100
Message-ID<G5UIV-ahmd-1@gated-at.bofh.it>
In reply to#255600

[Multipart message — attachments visible in raw view] — view raw

My take:

I use aliases heavily in my shell (bash), but I rarely use bash scripting
at all.

For any serious scripting, since about 2016 I'ved used Raku (formerly Perl
6). Before that I used Perl. Both are much easier to use.

-Tom

[toc] | [prev] | [next] | [standalone]


#255620

FromKen Young <ken@highwinds.cloud>
Date2023-03-05 12:40 +0100
Message-ID<G5USC-ahpE-1@gated-at.bofh.it>
In reply to#255618

[Multipart message — attachments visible in raw view] — view raw

Is perl6 production ready?
I have not used perl for a long time.


Sincerely,
Ken Young


On Sun, Mar 5, 2023 at 7:28 PM Tom Browder <tom.browder@gmail.com> wrote:

> My take:
>
> I use aliases heavily in my shell (bash), but I rarely use bash scripting
> at all.
>
> For any serious scripting, since about 2016 I'ved used Raku (formerly Perl
> 6). Before that I used Perl. Both are much easier to use.
>
> -Tom
>

[toc] | [prev] | [next] | [standalone]


#255621

FromTom Browder <tom.browder@gmail.com>
Date2023-03-05 12:50 +0100
Message-ID<G5V2i-ahtu-15@gated-at.bofh.it>
In reply to#255620

[Multipart message — attachments visible in raw view] — view raw

On Sun, Mar 5, 2023 at 05:30 Ken Young <ken@highwinds.cloud> wrote:

> Is perl6 production ready?
>

Yes, but please use its new name, Raku. Note new releases come out monthly
so you shouldn't use the Debian packages since they are way behind. We have
a member on the release team who provides Debian packages as "rakudo-pkg"
so apt/aptitude update/upgrade work as expected.

And the community is smart, friendly, and welcoming. Check IRC channel
#raku.

Start your journey at https://Raku.org.

Blessings,

-Tom

[toc] | [prev] | [next] | [standalone]


#255625

FromNicolas George <george@nsup.org>
Date2023-03-05 14:30 +0100
Message-ID<G5WB3-aixL-1@gated-at.bofh.it>
In reply to#255621

[Multipart message — attachments visible in raw view] — view raw

Tom Browder (12023-03-05):
> Yes, but please use its new name, Raku. Note new releases come out monthly
> so you shouldn't use the Debian packages since they are way behind. We have
> a member on the release team who provides Debian packages as "rakudo-pkg"
> so apt/aptitude update/upgrade work as expected.

That kind of precision does not really scream “production ready”.

If a language (or a library, a framework, whatever) cannot be used
without the latest experimental features added in the last two years,
then it might be worth toying with as a promising novelty, but we should
not use it for a real project, especially if we do not work alone or
intend to release the result.

So, what is it for Raku?

Regards,

-- 
  Nicolas George

[toc] | [prev] | [next] | [standalone]


#255627

From<tomas@tuxteam.de>
Date2023-03-05 17:10 +0100
Message-ID<G5Z5T-akds-7@gated-at.bofh.it>
In reply to#255625

[Multipart message — attachments visible in raw view] — view raw

On Sun, Mar 05, 2023 at 02:19:38PM +0100, Nicolas George wrote:
> Tom Browder (12023-03-05):
> > Yes, but please use its new name, Raku. Note new releases come out monthly
> > so you shouldn't use the Debian packages since they are way behind. We have
> > a member on the release team who provides Debian packages as "rakudo-pkg"
> > so apt/aptitude update/upgrade work as expected.
> 
> That kind of precision does not really scream “production ready”.

TBH, this is also one of the things which made me steer
clear of Raku. Interesting concepts, mind you, but when
I use Perl, I expect to be able to hand a program out
to people without limiting too much which binary version
and which package versions they have to use.

Just using a "reasonable new" Debian should work.

I watch Pythons venv hell (and have to poke at it sometimes)
with some disgust.

Cheers
-- 
t

[toc] | [prev] | [next] | [standalone]


#255674

FromTom Browder <tom.browder@gmail.com>
Date2023-03-07 13:10 +0100
Message-ID<G6EiJ-aM57-11@gated-at.bofh.it>
In reply to#255625

[Multipart message — attachments visible in raw view] — view raw

On Sun, Mar 5, 2023 at 07:20 Nicolas George <george@nsup.org> wrote:
...

> Tom Browder (12023-03-05):
> > Yes, but please use its new name, Raku. Note new releases come out
> monthly

> so you shouldn't use the Debian packages since they are way behind. We
> have


I shouldn't have said "you shouldn't use the Debian packages." I've been a
core developer since 2016 and am automatically primed to use the latest
release. The Debian version should be fine for production use.

That kind of precision does not really scream “production ready”.
>
> If a language (or a library, a framework, whatever) cannot be used
> without the latest experimental features added in the last two years,
> then it might be worth toying with as a promising novelty, but we should
> not use it for a real project, especially if we do not work alone or
> intend to release the result.


The regular releases are generally speed improvements or new features--in
general, nothing that changes any exising Raku code. Any necessary changes
are done after a long deprecation period where the user can have adequate
warning and preparation time to update his existing code.

Try it, you'll like it. I like for many reasons. Among them:

+ it is C-like in its use of curly braces (and not weird whitespace like
Python, ugh)
+ its kebab-case as in:
           my $first-name = 'Tom';
+ easy use of classes
+ much easier to use than Perl (much less so-called "line noise")
+ huge set of built-in routines
+ easy to create and use public add-on modules
+ great built-in math capability

New users don't have to use the great power available all at once. It's
easy to get started building practical things. If I were building GnuCash
from scratch, I would use Raku and JSON instead of C, Scheme, and XML.

I hope to see you on IRC #raku.

Best regards,

-Tom

[toc] | [prev] | [next] | [standalone]


#255623

From<tomas@tuxteam.de>
Date2023-03-05 14:10 +0100
Message-ID<G5WhH-aiqU-1@gated-at.bofh.it>
In reply to#255620

[Multipart message — attachments visible in raw view] — view raw

On Sun, Mar 05, 2023 at 07:30:18PM +0800, Ken Young wrote:
> Is perl6 production ready?

Of course. But be aware that it is quite a different language.

> I have not used perl for a long time.

If you prefer the "old" Perl (I do, for... reasons), it's still
being maintained actively, too.

Cheers
-- 
t

[toc] | [prev] | [next] | [standalone]


#255650

FromYassine Chaouche <a.chaouche@algerian-radio.dz>
Date2023-03-06 11:10 +0100
Message-ID<G6fX3-aw50-7@gated-at.bofh.it>
In reply to#255578
Le 3/4/23 à 09:22, Ken Young a écrit :
> Hello,

Hi Ken!

> 3) the content of bash script
> 
> $ cat get.sh
> 
> #!/bin/bash
> 
> 
> source ~/.bash_profile
> 
> k get node
> 
> 
> 4) alias can't work
> 
> $ ./get.sh
> 
> ./get.sh: line 5: k: command not found
> 

Two options :

1. either use #!/bin/bash -i as your shebang.
    The -i will automatically enable aliases
    (they are disabled by default in non-interactive scripts)


2. or use shopt -s expand_aliases in your script,
    like so :



$ cat get.sh
#!/bin/bash
shopt -s expand_aliases
source ~/.bash_profile
k get node
$

See what works best for you.

Best,

-- 
yassine -- sysadm
+213-779 06 06 23
http://about.me/ychaouche
Looking for side gigs.

[toc] | [prev] | [standalone]


Back to top | Article view | linux.debian.user


csiph-web