Re: [OT] bash help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Feb 20, 2017 at 10:46:23AM +0000, Andy Blanchard wrote:
> On 20 February 2017 at 09:18, T_POL <t_pol@xxxxxxxxxx> wrote:
> >
> > not sure about that but I think the "cd" command executes indeed
> > but it's valid only for the scripts' environment and not for the
> > shell you started the script from.
> 
> *ding* *ding* *ding*  We have a winner!
> 
> Shells execute in their own instance of bash (or whatever) - a
> subshell.  The way to do this is with an alias or function, depending
> on your needs (aliases don't except parameters, functions do), so
> here's the function for "mdcd":
> 
> $ mdcd() {
>     # additional checks here, per original post
>     mkdir $1
>     cd $1
> }
> 
> 
> Alternatively, you can force a script to run in the current shell by sourcing it
> 
> $ . {scriptname}
> 
> Note the "." between the prompt and script (you can also use the more
> literal "source"):
> 
> $ source {scriptname}
> 
> Another option, just for completeness, would be to alias sourcing a
> script, e.g.:
> 
> $ alias mdcd='. {path to script}'
> 

A caveat I think the OP already found out.  Both the sourced
script and the function would be running in the users
interactive shell environment, it must for the cd to be effective.
Thus you do not want to "exit" from either the sourced
script nor from the function as that causes the interactive
shell to exit.  In the function you can use "return <status>"
instead of "exit <status>".

Jon
-- 
Jon H. LaBadie                  jonfu@xxxxxxxxxx
_______________________________________________
users mailing list -- users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe send an email to users-leave@xxxxxxxxxxxxxxxxxxxxxxx



[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [EPEL Devel]     [Fedora Magazine]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Desktop]     [Fedora Fonts]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Fedora Sparc]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux