2

I'm writing a ksh script, and I'd like to use a command define by an alias in my .profile file, in my script. I tried this command in the folder containing my script, and it works. However, in my script, I've: [command] not found.

How can I solve this? My command just make a ls of a repository, then I'm doing in my script:

[command]| grep ... | { IFS== read -r var1 x && IFS== read -r var2 x; }
5
  • 1
    Why are you defining aliases in your ./profile? Why not ~/.kshrc which would solve your problem?
    – terdon
    Commented Nov 18, 2013 at 16:09
  • Actually, I'm in a professional service, and I can't do this. I can only use ./profile. Commented Nov 18, 2013 at 16:10
  • 2
    Then either source your .profile from your script, or define the alias in your script or don't use the alias but the actual command. Why in the world can't you use ~/.kshrc? That makes no sense, if you can use .profile you can use .kshrc. Aliases have no business being defined in .profile.
    – terdon
    Commented Nov 18, 2013 at 16:13
  • @terdon /me shrugs. I use .profile to define shell-agnostic things, and then source it. see unix.stackexchange.com/questions/88201/…
    – strugee
    Commented Nov 18, 2013 at 16:38
  • to reiterate some of the sentiment above, using aliases in a script is a bad idea from the get-go. if you know what that alias is running, why don't you do something like this : (say alias is as such "mycmnd=/usr/sbin/grep") in your script you can use MYCOMMAND=/usr/sbin/grep and then everytime you need to use this command, you can reference it by $MYCOMMAND.
    – MelBurslan
    Commented Nov 18, 2013 at 19:56

1 Answer 1

2

Don't use aliases in your scripts. This is a bad idea for exactly this reason. There are various ways to work around this:

  • Define the alias within the script itself
  • Source the file that contains the alias from the script. Add this line to it:

    . /home/your_user/.profile
    
  • Use the command itself instead of the alias. For example, if you have alias foo="echo bar", use echo bar in your script instead of foo.

As a general rule, it is a bad idea to set aliases in .profile. That file is only read by login shells, not interactive ones and not when running scripts. To make your aliases easily accessible you should add them to $HOME/.kshrc. The following is from man ksh:

If the shell is invoked by exec(2), and the first character of argument zero ($0) is -, then the shell is assumed to be a login shell and commands are read from /etc/profile and then from either .profile in the current directory or $HOME/.profile, if either file exists. Next, for interactive shells, commands are read from the file named by performing parameter expansion, command substitution, and arithmetic substitution on the value of the environment variable ENV if the file exists.

In other words, login shells read /etc/profile and .profile while interactive shells (what you get when you open a terminal) read whatever is in the $ENV variable or, if that is not defined, ~/.kshrc. This means that aliases in your .profile will only be read when sshing into the machine or otherwise starting a login shell.

2
  • You forgot the "workaround" of using proper functions :-) "For almost every purpose, aliases are superseded by shell functions." (from the bash manual).
    – Kusalananda
    Commented Feb 3, 2018 at 14:43
  • @Kusalananda you would still have to source the file where the function is defined, so that's not an easy solution either.
    – terdon
    Commented Feb 3, 2018 at 14:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .