Open
Close

What is Cygwin. Installing and configuring Cygwin How to save command history in bash cygwin

This article is intended for two
types of people: for those who do not understand
Linux, but can't install it on my computer
(there are different reasons), or for those who
knows a little about penguins 🙂 and wouldn’t mind
get to know them on a "neutral"
territory, that is, on Win98/NT.

The site has already described Cygwin as the best
take out the Linux emulator, but it was not said
nothing about installation, configuration and bugs of Cygwin.
Here I’ll tell you about it :) Actually, if
come to think of it, Cygwin is not an emulator at all, but
just a Linux distribution written for Win32 😉

Stop rambling... Let's go to http://www.cygwin.com,
We take the installation program setup.exe from there. To her
we will pull already precompiled ones
binaries (in exe extension), do you remember rpm?
😉 from the developer’s website, or from a mirror.
I RECOMMEND this program only to download
We will install these files manually, because If
you install using setup.exe, gcc does not work - fig
knows why... Maybe the developers have already taken into account
this is a bug, but I don't know about it yet...

So, download all the main binaries (sources
you don’t have to download), and several
additional - who would refuse wget, lynx,
BitchX, Python :) But you don’t have to download Perl... I have
there was ActivePerl - an advanced Perl emulator. Download
available from http://www.acriveperl.com.
I didn’t observe any glitches :) (I’ll tell you how I
implemented it in cygwin).

Settings

And in the "group" file in the same "etc" folder:
root:x:0:root

If you want to encrypt your password, and
not "password" as I did in my example,
write a Perl script

#!/usr/bin/perl
chomp ($pass= );
print $crypt("SA",$pass);

Where, SA - salta, which will be used for encryption
password... you can make it random, but
This article will be off-topic.

Now edit the file "c:\cygwin\cygnus.bat" -
in it we change PATH, simply changing the path to
emulator and the path to the login.exe file. Let's launch
file.

Let's log in: in my example, "root": "password".
So we got into the emulator itself. I don't
I liked the input prompt. Let's change it
- variable PS1. Open (create) the file "c:\cygwin\etc\profile"
and write there:

# /etc/profile
PROFILE_LOADED=1
PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/usr/local/sbin:$PATH"
unset DOSDRIVE
unset DOSDIR
unset TMPDIR
unset TMP
export HOSTNAME=test
PS1="[\u@$HOSTNAME \W]# "
LOGNAME=$USER
export PATH PS1 USER LOGNAME

Here... Then we’ll re-login, and everything will be on
place and beautiful :) The invitation will be: # - standard, Linux :)

That's it, the initial installation is complete.
Let's move on to installing services :)

The standard delivery includes thuja hucha
services. You need to run them like this:
/usr/sbin/inetd -R /usr/sbin/in.ftpd (example for ftpd).

It’s better to install SSHD yourself - it worked with a bang and
I drive from SecureCRT, legal on 127.0.0.1 -
completely different look :)

As for working with TCP/IP, I have no disadvantages
I noticed :) Everything connects and works like
necessary...

And now some tips:

1) Access to ALL disks is open. So be
careful. For example, drive d: is located here -
"/cygdrive/d".

2) Regarding Perl, create a symlink to the file
Perl.exe, for example, so "ln -s /cygdrive/c/perl/bin/perl.exe
/usr/bin/perl"

3) If for some reason you don’t have
installing a Linux application -
just fix the C code. May be,
will succeed.. (this is how I compiled
micq).

4) Follow Cygwin updates on the website http://www.cygwin.com
- binaries are updated frequently.

Questions? Write to me by E-Mail - I'll try
help...

Developing cross-platform applications is not easy. It is especially difficult thanks to Windows, in which everything is not like in humans. Here you have neither a normal terminal, nor normal console utilities, nothing at all! Once again faced with this problem, I decided to try Cygwin again. And guess what, it turns out that it actually makes Windows something that you can almost use!

You can download the Cygwin installer from here. In fact, this is not so much an installer as a package manager, which, when launched, directly asks which packages you would like to install. Guys! Package Manager! Under Windows! The installer creates a cache in the directory from which it will be launched. Therefore, before starting, it is better to create some special folder on the desktop and put the installer in it.

What packages in my opinion need to be installed:

  • openssh- masthev;
  • inetutils, bind-utils, ping, nc And nc6- masthev;
  • wget, curl- masthev;
  • python and/or python3— I think it’s a must-have;
  • vim, gvim— for editing small files it will not be superfluous;
  • diffutils, pwgen, unzip, rzync- let them be just in case;
  • gcc and/or clang, cmake, gdb- if needed;
  • screen, tmux, mc- for lovers;
  • git- under very big question, see below;

Note! To install pip in Cygwin, check the box next to the python-setuptools and/or python3-setuptools package. After installation, in the terminal say:

easy_install-2.7 pip

easy_install-3.4 pip

..., depending on what version or versions of Python you installed.

So, after installing Cygwin, you can open a terminal:

Guys! A normal terminal that resizes allows you to copy and paste text, and that's all! Under Windows! With all the well-known and familiar commands id, ls, pwd, cat, ssh, scp, and others. With the human environment - .bashrc, ~/.ssh/config, and so on. Plus, unlike the shameful hack called PowerShell, Cygwin does not drag along the .NET Framework. We can even open multiple terminals and switch between them using the Ctr + Tab shortcut. It's almost like tabs! Of course, the good old Alt + Tab also works.

Above I did not recommend installing Git, and here's why. The fact is that in Cygwin it slows down noticeably. It is recommended to run the commands:

git config --global core.preloadindex true
git config --global core.fscache true
git config --global gc.auto 256

But this, unfortunately, does not solve the problem completely. So, on a large project, the git status command can be executed in 20 seconds. In Linux, on exactly the same repository, the command runs instantly.

Those who like to work in a console similar to a Linux console can install Cygwin, a Unix-like environment where standard console programs from Linux will be available.

Installing Cygwin is quite simple. During the installation process in one of the modes, you can check the boxes next to the software packages that you want to install. I chose packages nano, git and something else.

After installation, a Cygwin shortcut appears on the desktop to launch the environment.

Run Cygwin from any folder

Since I use TotalCommander, it is convenient for me to launch programs immediately from the directory I need. Let's create a convenient bat file to launch Cygwin.

Immediately, without much thought, copy the string “Object” from the property of this shortcut into the new file we created C:\bin\bash.bat
The full contents of the file will look like this:

@start "" C:\cygwin\bin\mintty.exe -i /Cygwin-Terminal.ico -

Please note that the path to the exe file may be different for you. I installed Cygwin in the C:\cygwin directory.
Also, if you have not yet added the C:\bin directory to your PATH environment variable, then you should do so.

Setting up Cygwin launch

After some work with Cygwin, I discovered that although I can launch Cygwin from any directory by simply typing the word bash , the launch environment itself always opens in your user's home directory. (The home directory is set in the HOME environment variable).

This does not look very convenient, because if I write a bash command while in the D:\xyz directory, then I expect that the running Cygwin will also be in the D:\xyz directory.

If you pay attention, you will see that the desktop shortcut launches the mintty.exe utility. We immediately understand that we need to familiarize ourselves with the list of incoming attributes for this command.
Open Cygwin and enter

Man minty

After carefully studying the manual, we are pleased with some possibilities that we did not suspect about, namely: the ability to set the size and coordinates of the Cygwin window to be launched.

Open our bat file and change its contents to the following:

@start "" C:\cygwin\bin\mintty.exe -i /Cygwin-Terminal.ico --size 120.77 --position -4.0 -

In this case, I set the size of the Cygwin window I needed and the window's appearance position (a negative value of -4 is necessary so that the edge of the window is outside the border of the monitor and is not visible).

Now my window will always appear in the coordinates convenient for me, and will not shift by +8 pixels in x, y with each new launch.

But what about the Cygwin launch directory?

After all, going to the desired directory from your home directory is terribly inconvenient!

The solution has been found! To set the current directory when starting Cygwin, we can use the autorun script to run the bash environment.

So we need to improve our bash.bat file so that it creates an autorun file for the bash console to be launched

We launch Cygwin, enter the command “man bash” and read the documentation in search of what we need. We find several options for file names for the user’s home directory, which are launched when logging into bash.
Files run when logging in to bash:

~/.bash_profile ~/.bash_login ~/.profile

File launched when bash is started without being logged in

~/.bashrc

Important Note: The startup file ~/.bash_login (or any other) must be written in UTF-8 encoding! And should not use \r characters. (For example, the pwd command did not work for me when the file contained the \r character at the end, which is typical for the Windows system).

As it turns out, outputting UTF-8 text from CMD is not so easy, but it is possible!
Here's an example of how this is done:

Cmd /u /c chcp 65001 | echo some text >"filename"

Let's improve our C:\bin\bash.bat file, now it has the contents:

@echo off SET file=.bash_login cmd /u /c chcp 65001 | echo #!/bin/bash > "%HOME%\%file%" cmd /u /c chcp 65001 | echo cd "%CD:\=/%" >> "%HOME%\%file%" @start "" C:\cygwin\bin\mintty.exe -i /Cygwin-Terminal.ico --size 120.77 --position -4,0 -

Once launched, the bash console executes the contents of the ~/.bash_login file. This file must be in UTF-8 format. To do this, we call CMD with the /u flag, which tells us that the launched console should return the result in UTF-8 format.

As you can see, we are passing two commands separated by | . This sign allows us to write the two commands we need on one line.

The first command chcp 65001 sets the UTF-8 encoding in the running console. (To find out what the current encoding is set in the console, just call the chcp command without parameters.)

Second team echo text > "%HOME%\%file%" prints the corresponding text to a file named "%HOME%\%file% ", where the variable names are expanded into the path to the home directory and the file name .bash_login .

Please note the unusual access to the CD variable, which contains the current directory of the cmd console. If a variable is usually expanded by writing

Then in our case, we use the notation

%CD:\=/%

This allows you to immediately replace all Windows-style slashes \ with Linux-style slashes / .

Don't forget to put all file and directory names in double quotes, this will allow you to get into directories containing spaces.

Due to the fact that UTF-8 is used, this design successfully allows you to open Cygwin in directories with Russian names.

Thus, every time you run the bash command in the console, the script C:\bin\bash.bat will be launched, which will overwrite the startup file of the bash environment in Cygwin. Now we can easily and conveniently launch the bash console from any directory and immediately get to the same directory in the bash console.

We rejoice and enjoy the result!

Let's sum it up

  1. We installed Cygwin
  2. Created a file C:\bin\bash.bat with the contents @echo off SET file=.bash_login cmd /u /c chcp 65001 | echo #!/bin/bash > "%HOME%\%file%" cmd /u /c chcp 65001 | echo cd "%CD:\=/%" >> "%HOME%\%file%" @start "" C:\cygwin\bin\mintty.exe -i /Cygwin-Terminal.ico --size 120.77 --position -4,0 - (Note: the path C:\bin\ must be contained in the PATH environment variable)
    Due to this we have achieved:
    • launch a Cygwin terminal at a specified screen position ( --position option)
    • launch a window of a certain size (the --size parameter)
    • launch Cygwin from any directory by typing bash in the console
    • at the same time, the launched Cygwin is located in the same directory from which it was launched!

What is CYGWIN After Chris Kaspersky’s article “How to make Unix out of Windows” in Group No. 3 dated January 23, 2001, I was eager to tell Windows users a little more about what Cygwin is. I have been using it for quite a long time and can no longer work effectively in Windows without Cygwin installed.

The word Cygwin consists of two components: Cygnus is the name of the company, and Windows, but it would be wrong to say that everything in Cygwin was written by the guys from Cygnus. Cygwin is a port of Gnu utilities for Windows, and Gnu (www.gnu.org) is a project of the Free Software Foundation (or simply FSF), with the goal of creating a non-profit, non-owned and free Unix system from licenses that restrict freedom of distribution and modification of software. As part of this project, almost all standard UNIX utilities were rewritten. The three most significant contributions of the FSF are gcc (the Gnu C Compiler, or Gnu Compiler Collection), Bash (the Bourne Again Shell), and Emacs. Without gcc there would be no Linux, and Bash is the standard shell for many modern Unix systems. People with experience say that the first thing system administrators do after installing commercial Unixes is to reinstall Gnu utilities. In principle, GNU and FSF themselves are a vast topic and deserve a separate detailed article.

The entire set of Gnu utilities is a set of small (usually) programs that cover most of the tasks that a user of a Unix system (and not only Unix) encounters every day; each such program performs one task, and performs it well. These programs can then be combined to solve various problems.

Attempts to port Gnu to other, non-Unix platforms have been made more than once, which, by the way, indicates their popularity.

It is worth mentioning the project of porting utilities to DOS - DJGPP. Go to http://www.delorie.com/djgpp/, it's really interesting.

Cygnus did not compile each utility separately, suffering each time to solve the same problems; instead, a kind of “spacer” was written between the GNU utilities and the operating system. This shim - cygwin1.dll (1 - version number) - provides emulation of UNIX system calls, which allows you to compile and execute Unix programs with little or no changes to the source code. Basically, this dll is Cygwin itself, and everything else is GNU software packages compiled to work with Cygwin.

The presence of gcc and libraries makes it possible to port a wide variety of applications to Windows and makes Cygwin a full-fledged platform - you can sometimes find the expression "Cygwin platform". Since the first beta versions of Cygwin, various people have compiled for Cygwin what they lacked for Windows, and the standard Cygwin distribution has gradually grown, including through these home-made compilations. For example, during Beta 20 I had to download the vim editor separately, but now it is installed along with the entire set.

How to install it all

Cygnus has written a convenient program for managing packages, slightly reminiscent of some similar programs from Linux OS distributions. The first step is to download it from the Cygnus website http://sources.redhat.com/cygwin/setup.exe. By launching it, you can download a standard set of packages from one of the many mirrors, and you can choose to do this selectively, skipping individual packages.

For the future - by launching this program later from the same directory, you will be able to see what new packages have appeared or new versions of existing packages.

The same program will install Cygwin for you, again asking you to decide what to install and what not to install. The installation will create directories for you that are common in the Unix file hierarchy - bin, usr, sbin, etc., so it is convenient to place them all in one directory so that they do not get confused with your Windows folders.

And how to use it all

When you start Cygwin, you will see a console window similar to a regular Windows console, command.com in Win9x or cmd.exe in NT. But no! On Windows you launched a glorious descendant of Unix command interpreters, which tried to imitate command.com with its BAT files. Bash and its older brother sh are the glue that binds the Unix system together; it can rightfully be called a programming language, that is, you can write in it.

Several basic commands and their equivalents in Windows

ls = dir - view the contents of the directory;
cd = chdir - move to another directory;
cp = copy - copy a file;
mv = mv - move/rename file;
rm = del - delete file;
mkdir = mkdir - create a directory;
pwd - display the current directory.

Remember - the big "A" is no longer equal to the small "a", the \ symbol must now be replaced with /. The cd / command in Cygwin will take you to the directory where bin, usr, etc. are located. In Unix there is nothing above /, but in Cygwin, in order to travel through Windows partitions, you can use the cd //c/ command (cd //d/MyDir, etc.).

One of the most convenient features of Bash is autocompletion. Let's create several files with the cat command:

cat > first_file ENTER

type something, then Ctr-C,

and the same for another file:

cat > second_file ENTER.

Make sure the files exist:

Now type any command, for example, ls, and the first letter of the first file. Press TAB. Bash will append the file name itself. If the files start the same, such as file_one and file_two, Bash will append only file_ and wait for you to type another letter to disambiguate it.

Several examples of using some utilities.

Let's say you have a text file in.txt. You can count the number of words in it like this:

Number of lines:

Find lines containing the character sequence Larry:

grep "Larry" in.txt

Split lines into words, replacing spaces with end-of-line characters:

cat in.txt | gawk "gsub(" +","\n")"

The gawk command "gsub(" +","\n")" means to replace one or more spaces (" +") with the end-of-line character ("\n").

Do the same, but with the possibility of convenient viewing (PageUp, PageDown, exit - q)

cat in.txt | gawk "gsub(" +","\n")" | less

Sort strings (words):

cat in.txt | gawk "gsub(" +","\n")" | sort | less

Remove identical lines (words):

cat in.txt | gawk "gsub(" +","\n")" | sort | uniq | less

cat in.txt | gawk "gsub(" +","\n")" | sort | uniq | wc -l

Remove identical lines (words), counting their number:

cat in.txt | gawk "gsub(" +","\n")" | sort | uniq --count | less

Sort again, thus obtaining a frequency dictionary of the text:

cat in.txt | gawk "gsub(" +","\n")" | sort | uniq --count | sort -r | less

The same thing, with the results output to a text file:

cat in.txt | gawk "gsub(" +","\n")" | sort | uniq --count | sort -r > out.txt

The same thing, with intermediate results saved in the intermediate.txt file:

cat in.txt | gawk "gsub(" +","\n")" | teeintermediate.txt | sort | uniq --count | sort -r | less

Now, using the power of Bash, you can repeat this command for multiple files. Type the script directly into the console, going to a new line as printed in the article - Bash will understand that the command is not finished and will change the prompt to ">":

for file in *.txt; do

cat $file | gawk "gsub(" +","\n")" | sort | uniq --count | sort -r > $file.out

Like this. Simple problems - simple solutions.

To find out how a particular command works, you can run it with the --help switch or use the man (manual) command - man awk, man grep. You can exit man using q.

WHAT ELSE IS IN CYGWIN

Compiler

A simple (people say - 1 day of training) word processing programming language, one of the ancestors of Perl

Stream editor

Parser generator in C

Parser generator in C

Programming language. Actually, it is not directly related to Gnu utilities; it was supported by the Sun corporation for some time. You can do cd /usr/share/tk8.0/demos and run the program widget -./widget. You will see a demonstration of the capabilities of Tk, a Tcl extension for creating GUIs.

Archiver

Powerful archiver

File comparison program

And a lot more. Just go to bin and see what you have there. Package deposits can be found at http://www.hirmke.de/software/develop/gnuwin32/cygwin/porters/Hirmke_Michael/GNUWin32-contents.html. There is also a lot of information and links on the topic on the Cygnus website http://sources.redhat.com/cygwin/.

How to set up CYGWIN

First, you don't have to run Bash if you're intimidated by the console. You can use all the programs from Far, Windows Commander, etc., you just need to specify the path to the bin directory. In Windows 9x this is done in the autoexec.bat file, in NT in My Computer / Properties / Environment you need to add the path to bin to the PATH environment variable.

For more convenient work in Bash, I advise you to do the following: if your Cygwin directories are located in c:\cygwin, create a directory c:\cygwin\root (mkdir /root) and add a line to the cygwin.bat file that is in c:\ cygwin:

HOME=d:\cygwin\root

In the root directory you should create 2 files starting with a dot: .inputrc and .bashrc. Both files are executed by Bash on startup, .inputrc is responsible for ensuring that all keys work correctly, and .bashrc contains various information. As an example, I’ll give my .bashrc:

export PS1="\w > "

export PATH=".:$PATH"

alias ls="ls --color"

alias untar="tar xvf"

echo Welcome to CygWin!

The first line specifies the Bash path. This is what Unix does with all interpreters. For example, if you have a hello.pl script in Perl, and the perl itself is in /usr/bin, you can write on the first line:

and run it like this:

This way, you can run programs without worrying about what they are written in.

PS1 is a variable that stores the Bash prompt, the invitation to work, as it is also called. \w is the current directory.

After adding this to PATH, you can do it the way Windows users are used to doing it:

Next, I use synonyms, or aliases. For example, the ls command by default does not color its output depending on what the files are; to do this, you need to run it with the --color switch. This is inconvenient. The alias ls="ls" command --color" I solve this problem. Now ls means ls --color. It is not necessary to use the same command name, you can do ll="ls --color" - and use the new ll command. This is approximately what I do when creating the untar command to unpack tar archives, instead of typing tar xvf each time, you can also place pipes (pipes, pipelines) in alias: alias sort_un_sort=" sort | uniq --count | sort -r ".

In reality, the scope for arranging a workplace here is limitless, and my examples are primitive and simple. There are also functions, interactive commands in .bashrc, functions that run from the prompt...

I hope you are inspired to experiment with Cygwin. Send your comments to

In order to write applications for Unix it is not necessary to install it (under the word Unix I will think through everything *nix systems). This statement sounds rather paradoxical, but it is true, and in this article I will try to convince you of this.

If you are not a true Unix guru, then in order to write a couple of applications for this OS or compile existing programs you do not need to install Unix. An emulator is perfect for these purposes. Unix. There are many great programs that do this task perfectly, for example: UWin, MinGW, Cygwin and etc.

Suitable for compiling simple programs: MinGW, but for more complex ones it is not suitable. Uwin is a more advanced emulator, but its functionality is significantly inferior to another more popular emulator Cygwin.

Cygwin is a very functional, free emulator Unix. It is distributed as a single file ( setup.exe), don't be fooled by the size of this file ( ~300 kb), this is not himself Cygwin, but only its installer. All necessary utilities and libraries will have to be downloaded separately during the installation process, or you can specify a local directory as the source if you already have the necessary files.

Cygwin includes a huge number of different utilities and libraries, many of which you will probably never need, but when installing Cygwin allows you to select only the components you need. Thus, if you only want to compile the program, then you will only need to install the compiler GCC, a set of necessary libraries and utilities make. Personally, I installed all the applications because Cygwin makes it easy to “get rid” of them at any time. In fact Cygwin consists of just one file cygwin1.dll, everything else is software packages GNU, specially recompiled to work with Cygwin.



I think there were no problems with the installation. Now let's look at a few subtleties in setting up and using Cygwin. After installation, the structure of the folder in which the program was installed will resemble the structure *nix systems. In order to launch Cygwin, just run BAT file, cygwin.bat, which is located in the root directory Cygwin(by root directory we mean the folder in which the Cygwin, in the future we will count from this catalogue). After launch BAT file A console application will appear in front of you. This is the one Bash, which is the link between Unix And Windows. Bash- this is not just an analogue cmd.exe. Bash is a full-fledged programming language in which you can write scripts to perform various tasks. This is precisely what makes it fundamentally different from cmd.exe (command.com).

Invitation Bash, practically no different from cmd.exe (~ - means your home directory). There are no logical drives here (A, C, D, etc.), the countdown is from the root directory designated " / ". You also need to remember that case is important here, so Etc And etc- two completely different catalogs. Let's try to go to the root directory, to do this, just run the command familiar to you " cd/" (~ should change to / ). After that, type the command " dir", or " ls", in order to view the contents of the root directory. I think that now you understand what the root directory is.





I cannot describe all the commands in this article, so let’s move straight to the most important stage, for which we actually set Cygwin. Let's try to compile some program. For compilation I chose the program Htop(http://freshmeat.net/projects/htop/), this is an improved version of the standard program top, which is a console process manager. The first step is to unpack the archive. You can do this as in Windows, and directly in Cygwin. IN Cygwin this is done with the command:

tar zxf htop-0.6.tar.gz

Of course, at the moment you must be in the same directory where the archive is located htop-0.6.tar.gz. After unpacking, go to the created directory htop-0.6 and execute three standard commands to compile the application.

./configure
make
make install



That's it, the compilation is complete. Feel free to recruit a team htop and admire the result. The resulting "EXE" is located in the directory /usr/local/bin/.



Now let's try to write a simple program ourselves ( Hello world) and compile it with GCC. First we need to write the program itself, we will do this using the program cat. Enter the command cat new.c, Where new.c the name of the file containing the source code. After executing the command, write the following code:

#include
int main())(
printf("Hello World");
return 1;)

After the code is entered, press Ctrl+C. Now let's compile it using GCC. This is done with the command:

Gcc new.c -o new.exe

In the same directory where it was located new.c another file will appear - new.exe. When you run it, you will see the expected message " Hello World".



This could be the end of the article, but I decided to also talk about the graphical environment Cygwin so as not to return to this topic later. I think you noticed that when installing packages Cygwin suggests installing shells Gnome And KDE. In fact, these are not just shells, but their libraries, which are necessary to run applications. For example, a graphical version of the famous editor emacs, will run directly under Windows as if it were win32 application. In order not to be unfounded, let's try this one Emacs run. This is done with the command:

xemacs

This is what we will see.



As you can see, everything starts up and works fine. But this doesn't happen to everyone. The graphical environment is very capricious. Therefore, you need to be able to configure it. Setup Documentation X servers There are a lot of them on the Internet and you can easily find it.

In the end I would like to remind you that no matter how good Cygwin, it's just an emulator. And as you know, a copy cannot be better than the original, so if you want to become a real Unix guru, do not be lazy to install a full-fledged Unix distribution kit. In my opinion it is better to start with Linux. From all possible distributions I recommend you Slackware.