Configuring Cygwin for ConEmu with different start directory - windows

I am using ConEmu 64bit with Cygwin 64bit on a Windows 7 machine.
I don't want to set the working directory for cygwin in my .bashrc, as I like to setup more than one Task for Cygwin with different path parameters.
I set up the "home directory" in the task parameters:
\dir "D:\Downloads\Programming\Selenium\"
and added this line in the commands window:
C:\cygwin64\Cygwin.bat --login -i
Nevertheless Cygwin does not start in the "cygwinized" version of the given directory (/cygdrive/d/Downloads/Programming/Selenium) but in "normal" home directory of my user (~).
I only found howto's on how to add a new path in bashrc. Maybe one of you can enlighten me.

First of all, have you look at contents of your C:\cygwin64\Cygwin.bat? Why not?
#echo off
C:
chdir C:\cygwin64\bin
bash --login -i
Obviously, Cygwin.bat will never open bash in your desired dir.
Also, Cygwin ignores user defined startup directory! That is because cygwin always do cd "${HOME}" from /etc/profile script.
But, for example, bash from msysgit works properly.
Solution
However, /etc/profile script checks for CHERE_INVOKING environment variable before CD doing. So, the proper command line for starting cygwin will be:
set CHERE_INVOKING=1 & c:\cygwin64\bin\sh.exe --login -i
Another workarounds you may find in the project wiki page.

I had a similar demand (opening a new Cygwin-Tab in the folder currently viewed in explorer via the context menu) and found the following solution:
As stated by Maximus the Cygwin-Bash is usually opened by C:\cygwin64\Cygwin.bat which includes the login process. My approach was to change the working directory through the login.
.bash_profile:
if [ -f "${HOME}/startup.sh" ] ; then
source "${HOME}/startup.sh"
fi
startup.sh
cdc "D:\Downloads\Programming\Selenium\"
#cdc is a custom function, see .bashrc
.bashrc
cdc()
{
# converts a double-quoted windows-path and changes directory to it
p=$(cygpath -u "$1")
cd "$p"
}
The login progress thus changes the path of you bash to the one you've set in startup.sh. You might of course just type a Cygwin-compatible path in startup.sh without using the custom-function of .bashrc. It's still quite useful, especially if you want to want to use a dynamic startup.sh.
More Dynamic-approach:
Create a Cygwin.bat in C:\opt\ConEmu\ConEmu with the following content:
Cygwin.bat
#echo off
C:
echo cdc %1 > C:\opt\cygwin64\home\%USERNAME%\startup.sh
:: Your ConEmu-Task-definition here
C:\opt\ConEmu\ConEmu64.exe /Single /cmd {Cygwin}
This one writes a cdc-command followed by the path you provide into your startup.sh and starts an new instance of the ConEmu-Task Cygwin. The task itself does not do anything but starting an instance of Cygwin in my configuration:
ConEmu Task {Cygwin}
"C:\opt\cygwin64\Cygwin.bat"
I invocate the Cygwin.bat through the context-menu of the explorer. Another way would be to make a Windows-Shortcut pointing to Cygwin.bat with you path appended.

Related

permanently add binary to path on mac os

I am trying to permanently add a binary to the path variable on mac os. I have read several posts and blogs, it just does not work.
The question: given a directory /dir which contains an executable foo, how can I make it such that I can execute foo in the commandline without having to type export PATH... etc., and without having to move the foo executable to the bin folder?
I know there exists several scripts that are run on startup and whenever you open a command prompt, I just cannot seem to find the correct one. If I manually execute the export PATH... command and then do foo, it works.
If the export command works for you, just add it to either ~/.zshrc (on zsh) or ~/.bashrc (on bash).

Shell (Bash) - Can I have fully portable .bash_profile / .bashrc / .bash_history files?

Background
I'm a Front End Web Developer that has started moving to PortableApps where I can; at least for the desktop machines I use (Windows) after building a machine and having to re-install Windows multiple times.
It's getting more and more important to use the command line with build automation tools, testing software with a CLI etc.
I have just got portable versions of Git (Bash) and ConEmu working from my Dropbox (but ideally this would work from USB too). This means I have access to a Unix shell on Windows with Git, but the .bash_profile (and .bashrc) I have saved I need to manually copy to the '~' (home) directory for each machine I use.
Question
Is there a way to link my portable console with bash files not located in the home directory of the user on each machine used?
For instance when my console opens and looks for these files, can I ask it to check a different directory without setting any config on each machine? And then get the .bash_history to save here too instead?
You can use symbolic links for .bash_profile and .bashrc:
ln -s /path/to/.bashrc ~/.bashrc
ln -s /path/to/.bash_profile ~/.bash_profile
And inside your .bashrc you can define where your history file is located:
export HISTFILE=/path/to/.bash_history
I don't think there's any way around having .bashrc and .bash_profile in your home directory. Unless you start bash with the --rcfile option:
bash --rcfile /path/to/.bashrc
There is also the system wide file located at /etc/bashrc.

locate my bash configuration file

Is there a way to find the name of the bash configuration file that is currently on use. I searched for the .profile or .bashrc but did not find them. However still I can print some of the env variables i.e
$HOME, $PATH
If you are using one of these sudo su -, bash --login, ssh user#host,
these are considered as login shell,
then you might want to try
/etc/profile
~/.bash_profile
~/.bash_login
~/.profile
Environment variables are stored in the each process itself.$HOME and $PATH is stored in the bash process itself.
So,First you have to find process id of your bash process.You can use ps command to get the process id of the process.
Then,
cat /proc/your_bash_process_id/environ
I hope this will helps you.
As some people said, those files are in the home directory.
(Screenshot from Wiki article.)
I'll tell you here how to find it (the explanation here is for Windows 10 only), which is my system.
You can quickly find and open some of those locations with the following commands (using bash console):
cd ~ ← It will take you to your home directory.
ls -a ← It will display files and folders, both visible and hidden.
explorer . ← It will open the home folder in Windows Explorer, which can be really useful. Look for the file/folder you need. Move to it using:
cd folder-name or open the file in the text editor using notepad file-name. For your case, you should find and open notepad .bashrc
Those files are in the user's home directory.
$HOME/.profile
$HOME/.bashrc

How can I cd to an alias directory in the Mac OSX terminal

Is there a way to get into an alias directory from shell with the command "cd" ? It always returns that "htdocs" isn't a directory.
Edit: I made the shortcut with the OS GUI -> rightclicked the htdocs directory and chose "Alias..." (i'm using a german OS if it's not alias maybe it's called shortcut in english?) then i moved it to my home directory (because my terminal starts from there when i open it).
All i want is to open my terminal and type "cd htdocs" so that i can work from there.
you can make symbolic link to it.
ln -s EXISTING_PATH LINK_NAME
e.g.
ln -s ~/Documents/books ~/Desktop/
Reference
Enter into a directory through an alias in Mac OS X terminal
All i want is to open my terminal and type cd htdocs so that i can work from there.
The easier approach is probably to ignore the links and add the parent directory of your htdocs directory to the CDPATH environment variable. bash(1) will check the contents of the CDPATH environment variable when you type cd foo to find the foo directory in one of the directories listed. This will work no matter what your current working directory is, and it'll be easier than setting symbolic links.
If the path to your htdocs is located /srv/www/htdocs/, then you could use CDPATH=/srv/www. Then, cd foo would first look for /srv/www/foo/ and change to it if it exists; if not, then it would look for foo in the current working directory and change to it if it exists. (This might get confusing if you have multiple htdocs directories on your system; in that case, CDPATH=.:/srv/www would let you change into a child directory easily but still use the /srv/www/htdocs/ version if no ./htdocs directory is present.)
You can add the CDPATH=/srv/www line to your ~/.bashrc file so it works every time you start a terminal.
I personally use this to quickly work in the directory which is present deep inside one of my Volumes in my Mac.
Open your ~/.bash_profile, create an alias to the directory by adding this:
alias cdh="cd /Volumes/Haiku/haiku/src/apps/superprefs"
Save it, restart your terminal. Now on typing cdh in your terminal should change the working directory to the one mentioned as the alias.
I am not sure how OSX exposes Alias links but since you are using bash you can just create a variable in your .bashrc file.
On its own line put:
htdocs=YourDirectoryPath/
Once you have restarted bash you can just type cd $htdocs
There is a old hint on macworld to do this in a way that is integrated with BASH: Enable 'cd' into directory aliases from the Terminal
Plus, here is an answer that uses this solution on superuser.
You may be able to use osascript to do this -- this command seems to work:
cd "`osascript -e "on run aFile" -e "set aFile to POSIX file aFile as alias" -e "tell application "\""Finder"\"" to return POSIX path of ( ( original item of aFile ) as text ) " -e "end run" path_to_my_Finder_alias 2>/dev/null`"
Basically this command is running an AppleScript that finds the destination path of the argument (path_to_my_Finder_alias) in a subshell, then wraps it in double quotes, and changes the directory to it.
Maybe someone with a little more bash expertise can turn it into a bash alias or function.
try:
alias cdgo=`echo cd /root/go/`
cdgo will run, then get command "cd /root/go/" and enter, and it will change your directory in current terminal process
It works on my centos, no test with osx

How to make a shell script global?

I am on Mac's OS 10.6, and I am trying to learn a thing or two about shell scripting. I understand how to save a shell script and make it executable, but I am wondering what I can do or where I can save the file to make it global (that is, accessible no matter what folder I am in).
For example, if I save a .sh file in the /Users/username/ directory and make it executable, I can only execute that script in that specific directory. If I navigate to /Users/username/Downloads, for example, I can't execute the script.
Also, any suggestions of resources for learning more about shell scripting would be helpful. Thanks
/usr/local/bin would be the most appropriate location. Mac OS X has it in the PATH by default
There are two ways to do it -
Put your script in usr/local/bin and make sure it is executable(chmod +x my_script)(This is already set in the path, you can check by doing an echo $PATH)
Create a folder in your home directory called bin. (For your personal scripts)
cd ~ (Takes you to your home directory)
mkdir bin (create a bin folder)
vim .bash_profile (to set path environment variable)
export PATH=~/bin:$PATH (Press i then add this line and then do esc and type :wq)
Now you can just type the name of your script and run it from anywhere you want.
** NOTE: If you want to run the script with a shortened command rather than typing your entire filename, add the following to your .bash_profile:
alias myscript='my_script.sh'
Then you can run the script by simply typing myscript. (you can sub in whatever alias you'd like)
Traditionally, such scripts either go in ~/bin (ie: the bin directory in your home directory) or /usr/local/bin/ The former means the script will only work for you, the latter is for scripts you want anybody on the system to be able to run.
If you put it in ~/bin, you may need to add that to your PATH environment variable. /usr/local/bin should already be on the path.
In mac operating system
Open bash ~/.bashrc file.
add path of your script in your bashrc file , using
export PATH="$PATH:/Users/sher.mohammad/Office/practice/practiceShell"
Open your ~./bash_profile file and add [[ -s ~/.bashrc ]] && source ~/.bashrc
open new terminal window
Now whenever you will open your terminal your script will be loaded
This one is super easy if you are familiar with your bashrc file! This will entirely use just your .bashrc file and takes 2 seconds to accomplish.
(I use Arch Linux Manjaro so I use .bashrc located in my home directory)
The code to be placed in your .bashrc file:
# Simple bashrc method to launch anything in terminal from any directory
YOURCOMMAND () {
cd /path/to/directory/containing/your/script/ && ./YOURSCRIPT
}
As you can see, first you use the simple 'cd' command and give it the directory of the scripts location, then use '&&' so that you can make the next command executed right after, and finally open your script just as you would normally! Super easy and saved right in your .bash file! :)
Hope I've helped someone!
Sincerely,
AnonymousX
On using bash shell, write that script as function and then put it to the .bashrc or source the file which containing that function by "source file_name"
Now execute the script by function call in the shell.
Either saving it in /usr/bin (or any other directory present in PATH) or editing PATH to include the directory you saved it in will basically make it run in any directory.
from the working directory of 'script.sh'" mv [script.sh] /usr/local/bin"( not tested but seems to be the least complex way IMO.)
You should put it in the global executable directory on your machine. I think that would usually be /usr/bin on Unix-based operating systems (this would however most often require super user privileges on that machine).
You could also put it in any other directory that is in the $PATH environment variable, although it would only work for those users who have that directory in that variable.
You can find the value of $PATH by typing echo $PATH in a shell. The directories are separated by :.

Resources