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.
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.
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
}
Related
Note this is sourced, so this is not a shell script.
I am not asking for how to enable alias in noninteractive shell. I did this: shopt -s expand_aliases.
The Bash version is 5.1.4.
How to recreate:
Create a file named "p":
linky(){
comdll="cat"
shopt -s expand_aliases
alias modavar="$comdll"
echo "$argin" | modavar #| getlinks "$argin" | sort -u
}
Then run
echo "source p ; linky https://duckduckgo.com" | bash --norc
Expected output:
https://duckduckgo.com
Actual output:
p: line 5: modavar: command not found
When I run this once it give me
linky(){ alias jkl=echo\ hel; jkl; }
linky
bash: jkl: command not found
But if I do this,
linky(){ alias jkl=echo\ hel; jkl; }
linky
linky(){ alias jkl=echo\ hel; jkl; }
linky
It gives me
hel
What is happening?
You can't define the alias inside a function and use it there. Consider these examples:
alias foo=cat
ffoo() {
echo abc|foo
}
fbar() {
alias bar=cat
echo abc|bar
}
ffoo #->prints abc
fbar #->prints command not found
I'm trying to call methods from file to file with structure like:
/root
/subDir
/subSubDir
inSubSub.sh
inSub.sh
inRoot.sh
Files contents:
inRoot.sh:
#!/bin/bash
source ./subDir/inSub.sh
subMethod;
inSub.sh:
#!/bin/bash
source ./subSubDir/inSubSub.sh
subMethod () {
echo "I'm in sub"
}
subSubMethod;
inSubSub.sh:
#!/bin/bash
subSubMethod () {
echo "I'm in subSub"
}
subSubMethod;
Result of running $ ./inRoot.sh
subDir/inSub.sh: line 2: subSubDir/inSubSub.sh: No such file or directory
subDir/inSub.sh: line 6: subSubMethod: command not found
I'm in sub
So, it works for the first call but doesn't work deeper.
btw: using . ./ instead of source ./ returns the same
How to do it right, if it's possible?
You must change your inSub.sh like that
cat ./subDir/inSub.sh
#!/bin/bash
var="${BASH_SOURCE[0]}"
source "${var%/*}"/subSubDir/inSubSub.sh
subMethod () {
echo "I'm in sub"
}
subSubMethod;
I have a file called ~/.gotorc and in it I have
alias goto_usr="cd /usr"
alias goto_bin="cd /user/local/bin"
Then in my .zshrc I have
source ~/.gotorc
goto() {
`goto_$1`
}
But when I run goto bin, it says goto:bin: command not found: goto_bin. But if I run goto_bin then it works and I go to the expected folder.
How come?
Instead of aliases you can define a single function goto like this:
goto() {
case "$1" in
usr)
cd /usr;;
bin)
cd /usr/local/bin;;
esac
}
Then use it as:
goto usr
goto bin
this script file name "1sr" and i can work in terminal ". 1sr"
i want to change directory "home/byram/workspace/1/src/com/seri/*"
#!bin/sh
f=$(basename $0 | tr -d "sr")
pth="/home/byram/workspace/$f"
my1=$(ls $pth/src/com/seri)
cd $etc/src/com/seri/$my1
after ". 1sr" command f variable set "bash"
how can i fix it?
I would suggest a function called "prj" to put in your .bashrc:
prj () {
cd /home/byram/workspace/"$1"/src/com/seri
}
Then use it like this
prj 1 # Switch to ...1/src/com/seri
prj 2 # Switch to ...2/src/com/seri
i add in .bashrc this lines:
wr (){
cd /home/byram/workspace/"$1"/w
v1=$(ls /home/byram/workspace/"$1"/src/*/*)
v2=$(ls /home/byram/workspace/"$1"/src/*)
v3=$(ls /home/byram/workspace/"$1"/src/)
echo "$v3.$v2.$v1"
}
works for any project eg. com.example.abc,org.samp.xyz
thanks for #chepner
On *nix, using BASH (etc) you ask the system where a command is located (etc) by using the 'type' shell built-in like this:
$ type cat
cat is /bin/cat
Is there an equivalent of this 'type' command in Microsoft PowerShell 2.0 ?
An equivalent is Get-Command.
PS C:\> Get-Command ls
CommandType Name Definition
----------- ---- ----------
Alias ls Get-ChildItem
Application ls.exe D:\usr\local\wbin\ls.exe
Application ls.exe C:\Program Files (x86)\Git\bin\ls.exe
Windows 10 Update:
Since I've posted this answer, it appears that the behavior of Get-Command has changed. To include all results (in the style of Un*x) type), now I need to pass the -All flag, like so:
PS C:\> Get-Command -All ls
CommandType Name Version Source
----------- ---- ------- ------
Alias ls -> Get-ChildItem
Application ls.exe 0.0.0.0 C:\Program Files (x86)\Git\usr\bin\ls.exe
As noted in a comment, this doesn't include the Definition column as was the previous behavior. I can't determine a command-line argument to add the definition column, but as noted by #voutasaurus in the comment below, one can use:
PS C:\> (Get-Command -All ls).Definition
Get-ChildItem
C:\Program Files (x86)\Git\usr\bin\ls.exe
Version information for reference (I odn't have the version information associated with the original answer text, but I'm guessing that it was Windows 7):
PS C:\> [System.Environment]::OSVersion.Version
Major Minor Build Revision
----- ----- ----- --------
10 0 15063 0
Get-Command has a -ShowCommandInfo parameter that does this. It also works for functions defined in $profile :
PS C:\Users\vp937ll> Get-Command l -ShowCommandInfo
Name : l
ModuleName :
Module : #{Name=}
CommandType : Function
Definition : Get-ChildItem | Sort-Object -Property LastWriteTime -Descending
ParameterSets : {#{Name=__AllParameterSets; IsDefault=False; Parameters=System.Management.Automation.PSObject[]}}
Since you tagged this with Shell, in addition to PowerShell's Get-Command, there's where.exe:
PS C:\> where.exe notepad
C:\Windows\System32\notepad.exe
C:\Windows\notepad.exe
The command just looks for a file with the specified name through the path:
PS C:\> where.exe readme.*
C:\Python31\README.txt
C:\Program Files (x86)\wget\README
C:\Program Files (x86)\SysinternalsSuite\readme.txt
Note that when calling this command from PowerShell, you must call it as where.exe because Where-Object is aliased to where.
Get-Command seems right, aliased as gcm
Just pipe it to Select-Object * aliased as select *
E.g., in $profile I have a function that opens total commander from pwd (it silently kills the existing instance first)
function start-totalCommanderhere {
$here = (Get-Location).path
kill -n TOTALCMD64 -ErrorAction Ignore
start "c:\totalcmd\TOTALCMD64.EXE" $here
}
Set-Alias tc start-totalCommanderhere
Here is all the info about the function — it however does not tell you directly that it is a function, you would need to query its proper name not alias to get that information from CommandType attribute. (than it would be same as type in bash which tells you hello_world is a function in case it is one)
▶ gcm tc | select *
HelpUri :
ResolvedCommandName : start-totalCommanderhere
DisplayName : tc -> start-totalCommanderhere
ReferencedCommand : start-totalCommanderhere
ResolvedCommand : start-totalCommanderhere
Definition : start-totalCommanderhere
Options : None
Description :
OutputType : {}
Name : tc
CommandType : Alias
Source :
Version :
Visibility : Public
ModuleName :
Module :
RemotingCapability : PowerShell
Parameters : {}
ParameterSets :
gcm <module_name> | select * gives you the path if you ask for a CommandType which is Application
▶ gcm ping | select *
HelpUri :
FileVersionInfo : File: C:\Windows\system32\PING.EXE
InternalName: ping.exe
OriginalFilename: ping.exe.mui
FileVersion: 10.0.19041.320 (WinBuild.160101.0800)
FileDescription: TCP/IP Ping Command
Product: Microsoft® Windows® Operating System
ProductVersion: 10.0.19041.320
Debug: False
Patched: False
PreRelease: False
PrivateBuild: False
SpecialBuild: False
Language: English (United States)
Path : C:\Windows\system32\PING.EXE
Extension : .EXE
Definition : C:\Windows\system32\PING.EXE
Source : C:\Windows\system32\PING.EXE
Version : 10.0.19041.1
Visibility : Public
OutputType : {System.String}
Name : PING.EXE
CommandType : Application
ModuleName :
Module :
RemotingCapability : PowerShell
Parameters :
ParameterSets :
But with gcm you do not get paths for imported modules — for that you need to use Get-Module | Select-Object Name, Path aliased as gmo | select name,path
▶ gmo | select name, path
Name Path
---- ----
assign-vault-keys-to-env-vars C:\Users\Admin\Documents\workspace\work.log\kb\powershell\assign-vault-keys-to-env-vars.ps1
CimCmdlets C:\Program Files\PowerShell\7\Microsoft.Management.Infrastructure.CimCmdlets.dll
decode-base64 C:\Users\Admin\Documents\workspace\work.log\kb\powershell\decode-base64.ps1
DnsClient C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\DnsClient\DnsClient.psd1
...