Alias created in shell-script does not work

Asked

Viewed 123 times

2

I’m trying to create a alias using a shell-script, but at the end of the execution the alias disappears.

#!/bin/sh

echo "Configurando ambiente...";
mkdir -p ~/.dna

alias teste="node $HOME/.dna/start.js"
echo "Exibindo alias criado";
alias teste

The above code creates the alias, because just below the last echo it is displayed, but when trying to execute the command teste in the terminal it does not find the command.

If I run the excerpt alias teste="node $HOME/.dna/start.js" directly in the terminal the alias works.

Does anyone know why this occurs and has some contour solution?

Thank you.

  • Personal because out of scope? It’s a scheduling question.

  • Probably the alias is not being saved,it exists only in the session that is currently, gives a export?

  • Not giving export no @Tuxpilgrim .

  • Take a look at this: https://stackoverflow.com/questions/2197461/how-to-set-an-alias-insida-bash-shell-script-so-that-is-it-visible-from-the-ou

  • Thank you @Tuxpilgrim was exactly what I needed. If you want to translate into the Brazilian community I will mark the answer as correct. Thank you. ;)

  • Oops, great that worked out! Solved using the source or the shopt -s expand_aliases?

  • @Tuxpilgrim using the source even.

  • Ready, when you can accept the answer @Hiago!

Show 3 more comments

1 answer

3


The alias you are creating is only inside the shell that is running the script, to persist the aliases you are creating is to use the source ./script.sh.When you execute the command source the contents of the file will run in the current shell, and the aliases will be persisted in the environment.

Obs¹: Response based in that Soen post.

Obs²: More about the command source see that link.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.