Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining two shell commands into a single command

In Bash, we can combine two shell commands cd and ls like this:

function cd {
    builtin cd "$@" && ls
}
#this will get a list of file after changing into a directory

Also this

mkcd () { mkdir -p "$@" && cd "$@"; }
#this will create and directory and change into it at once

Can we do similar stuff in Powershell? If so, I would like to make similar functions and put it in my $profile

Thanks for any help.
Steeluser

EDIT:

I realized this could be done from shell like this:

$> pwd|ls

    Directory: D:\ps

Mode                LastWriteTime     Length Name                                                                      
----                -------------     ------ ----                                                                      
d----          5/7/2011   9:40 PM            config                                                                    
d----          5/7/2011   9:40 PM            output                                                                    
d----          5/8/2011   3:37 AM            static                                                                    
-a---          5/8/2011   3:36 AM        485 create-static-files.ps1                                                   

This could be put in a profile like this:

function pl
{
    pwd|ls
}

and can be invoked from the shell

ps$ pl

    Directory: D:\ps

Mode                LastWriteTime     Length Name                                                                      
----                -------------     ------ ----                                                                      
d----          5/7/2011   9:40 PM            config                                                                    
d----          5/7/2011   9:40 PM            output                                                                    
d----          5/8/2011   3:37 AM            static                                                                    
-a---          5/8/2011   3:36 AM        485 create-static-files.ps1                                                   

But I could not figure out how to do the mkcd function.

like image 596
Animesh Avatar asked Jan 27 '26 22:01

Animesh


2 Answers

Something like this should work.

Function mkcd {
  mkdir $args[0]
  cd $args[0]
}

This is just an ordinary function in powershell. See http://technet.microsoft.com/en-us/library/dd347712.aspx for more.

like image 153
Zac Thompson Avatar answered Jan 29 '26 13:01

Zac Thompson


You may want also manage exception directory already exists and also return the directory object to your caller:

Function mkcd {
  if(!(Test-Path -path $args[0])) {
   mkdir $args[0]
  }
  cd $args[0] -passthru
}
like image 23
Emiliano Poggi Avatar answered Jan 29 '26 11:01

Emiliano Poggi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!