Created a file with the following content:

#!/bin/bash alias some='echo 2' 

I gave him the right to execute and nothing happens when the script is executed (no alias is created). What am I doing wrong? Bash version 4.3.46

  • one
    He launched a new bash, created an alias in it and immediately went out. What did you want? - Alexey Ten
  • @AlexeyTen, I wanted to add an alias. How can I add an alias to the current bash? - alvoro
  • man bash. Specifically, here you can simply write source a.sh - Alexey Ten
  • one
    In general, aliases are usually added to .bashrc / .bash_aliases - Alexey Ten
  • one
    @alvoro, for files launched through source, shebang is better to delete (it does not make sense), and make the files themselves non-executable. - 0andriy

1 answer 1

pseudo-variable, variables, functions — these are all properties of the shell process. they are inherited by the child processes, but the child process cannot affect the properties of the parent process.

when you “run” a script, a new (child) instance of the shell process is created, and after it completes, all its aliases, variables and functions are irretrievably lost (well, not saved, for example, to a file).


to ensure that commands from the script are executed within the current shell process (without creating a child process), you can use the built-in source command (or its synonym - . ):

 $ source скрипт 

or

 $ . скрипт 

in general, the definitions of all aliases, variables, and functions that should be available in each interactive shell session are usually placed in the ~/.bashrc , which is “invoked” with the source ~/.bashrc command at the end of the launch of a new interactive session.