Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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; }
share|improve this question
1  
Why are you defining aliases in your ./profile? Why not ~/.kshrc which would solve your problem? –  terdon Nov 18 '13 at 16:09
    
Actually, I'm in a professional service, and I can't do this. I can only use ./profile. –  user1058398 Nov 18 '13 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 Nov 18 '13 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 Nov 18 '13 at 16:38
1  
Put the alias definition in a separate file, and use the . command to read it in to your script. The ksh users at my site by convention used a directory ~/fun to place all files that defined shell functions. You can have your .profile read in those files using . as well. This way, common aliases and functions are factored out and appear in just one place. –  Mark Plotnick Nov 18 '13 at 20:56

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.