bash - 将两个 shell 命令组合成一个命令

标签 bash powershell powershell-2.0

在 Bash 中,我们可以像这样组合两个 shell 命令 cdls:

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

还有这个

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

我们可以在 Powershell 中做类似的事情吗?如果是这样,我想制作类似的功能并将其放入我的 $profile

感谢您的帮助。
钢铁用户

编辑:

我意识到这可以像这样从 shell 中完成:

$> 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                                                   

这可以放在这样的配置文件中:

function pl
{
    pwd|ls
}

并且可以从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                                                   

但我不知道如何执行 mkcd 功能。

最佳答案

这样的事情应该可行。

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

这只是powershell中的一个普通函数。参见 http://technet.microsoft.com/en-us/library/dd347712.aspx了解更多。

关于bash - 将两个 shell 命令组合成一个命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5924646/

相关文章:

powershell - 通过 Powershell 运行 AZCopy 并监控进度

c# - 从 C# 调用 PowerShell

azure - ARM模板: how to read output result in array format

powershell - 将用户添加到本地用户组

regex - 如何在bash中用另一个字符串替换可变长度的重复字符串?

linux -/bin/bash 版本太低,如何在登录时使用不同版本的 bash 获取点文件

成功运行 docker-compose 后 Bash 退出

powershell - Powershell 中导入/导出的对象类型更改

powershell - 添加xml属性 "xsi:nil"

linux - shell 脚本 wget 在用作 cron 作业时不起作用