0

I have a interactive Bash script, conozcoArrays.sh,

#!/usr/bin/bash echo -e "\nGive me their phone number?\n" read number TOGOes=("$(find ~/chicas -maxdepth 1 -iname "*$number*" -type d -execdir echo {} + | sed "s;./;${HOME}/chicas/;g")" "$(find ~/chulos -maxdepth 1 -iname "*$number*" -type d -execdir echo {} + | sed "s;./;${HOME}/chulos/;g")" "$(find ~/parejas -maxdepth 1 -iname "*$number*" -type d -execdir echo {} + | sed "s;./;${HOME}/parejas/;g")" ) for togo in "${TOGOes[@]}" do if [[ $togo != "" ]]; then echo $togo export togo && cd $togo && return 0 else echo "Haven't found her in my directories." && cd ~/chicas fi done 

that performs a search in my directories for a keyword, and if it finds anything it changes to this directory. For this reason I usually launch it sourcing it, like so . ~/CS/SoftwareDevelopment/MySoftware/Bash/pasion/conozcoArrays.sh

I also have another Bash script, todo.sh, that references 'conozcoArrays.sh':

#!/usr/bin/bash ita='\e[3m' end='\e[0m' echo -e "1. La conozco? 2. Search through websites for a given phone number and create a dossier. 3. {ita}escort-scraper.py{end}" read ch if [[ "${ch}" == '1' ]]; then . ~/CS/SoftwareDevelopment/MySoftware/Bash/pasion/conozcoArrays.sh elif [[ "${ch}" == '2' ]]; then "${HOME}/CS/SoftwareDevelopment/MySoftware/Python/escorts/search-no.py" elif [[ "${ch}" == '3' ]]; then "${HOME}/CS/SoftwareDevelopment/MySoftware/Python/escorts/escort-scraper.py" fi 

The issue is that when I enter 1conozcoArrays.sh is not evaluated, it is launched but it does not seem to be sourced - I expect to be in a different directory after the todo.sh script finishes but I'm not. How might I source conozcoArrays.sh from another interactive script?

6
  • Are you sourcing todo.sh in the first place?
    – muru
    CommentedApr 12, 2023 at 8:23
  • @muru No, I'm not. Should I?CommentedApr 12, 2023 at 8:31
  • What should happen and what actually happens? Your conozcoArrays.sh is being sourced, what makes you think it isn't? Are you expecting to be in a different directory after the todo.sh script finishes?
    – terdon
    CommentedApr 12, 2023 at 8:37
  • 1
    "I expect to be in a different directory after the todo.sh script finishes" for that to happen todo.sh must be sourced.
    – muru
    CommentedApr 12, 2023 at 8:48
  • 1
    Consider having todo.sh print the directory instead of cd-ing your calling shell to it so you can do cd "$(todo.sh)" instead of . todo.sh to avoid any undesirable side-effects from everything else that todo.sh might do beyond changing directory if you source it.
    – Ed Morton
    CommentedApr 12, 2023 at 17:16

1 Answer 1

3

Your script is launched, as you say, and also as you say, it isn't sourced because you are not sourcing it, you are executing it. This means that the script does change directory but only while it is running. When the script exits, your original shell hasn't moved directory.

If you want a cd command inside a script to affect the parent shell, the interactive shell from which you launch the script, you need to source it. So instead of running todo.sh, source it:

. /path/to/todo.sh 

And then it will behave as you want.

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.