Best Linux Text Editors

Best Linux Text Editors

You can choose between several text editors in Linux. Each editor has advantages and advantages.

1. Vi/Vim
Vi is a powerful and the most popular command-line-based editor. Commonly used for writing code and editing configuration files. First of all, the advantage is availability. Vi is always installed on any distribution. The second advantage is the consumption of system resources. One of the cons is non-intuitive, but short commands.

Vi has 3 modes: command, input, and last line mode. Command mode is the default.

2. Nano
Nano is WYSIWYG (what you see is what you get) editor and is installed by default in Ubuntu and many other Linux distributions. Action/commands are done in a CTRL and Key manner, for example, CTRL + X save a file. Features: Autoconf support, case-sensitive search function, auto-indent ability, regular expression search and replace.

3. Gedit
Gedit is the default text editor for the GNOME desktop environment. Gedit’s aim is simple and easy to use for beginner Linux users. Useful features are syntax highlighting, clipboard support, brackets matching, search and replace with support of regular expressions

4. GNU Emacs
Emacs is the extensible self-documenting editor. It provides an interpreter for Emacs Lisp. Main function: text editing including a project planner, mail and newsreader, debugger interface, calendar.

5. Leaf Pad
GTK+ based editor is popular among new Linux users because it is easy to use. It supports the codeset option, auto codeset detection, and Drag & Drop function. It does not provide syntax coloring.

Which text editor is best Linux?

12 Best Text Editors For Linux Distros

  • Sublime Text. Sublime Text is a feature-packed text editor built for u201ccode, markup, and prose.u201d It natively supports tons of programming languages and markup languages. …
  • Atom. …
  • Vim. …
  • Gedit. …
  • GNU Emacs. …
  • Visual Studio Code. …
  • nano. …
  • KWrite.

What are the most common text editors in Linux?

Top 10 Text Editors for Linux Desktop

  • VIM. If you are bored of using the default u201cviu201d editor in linux and want to edit your text in an advanced text editor that is packed with powerful performance and lots of options, then vim is your best choice. …
  • Geany. …
  • Sublime Text Editor. …
  • Brackets. …
  • Gedit. …
  • Kate. …
  • Eclipse. …
  • Kwrite.

What text editor comes with Linux?

Almost all Linux distributions, even older versions, come with the Vim editor installed.

What is the best text editor 2020?

10 best code editors for 2020

  • Visual studio code. Visual studio code commonly referred to as VS code, is one of the best code editors in the market. …
  • Sublime text. If you are looking for a very lightweight yet robust code editor, the sublime text is your option. …
  • Atom Editor. …
  • Notepad++ …
  • Bluefish. …
  • Brackets. …
  • Phpstorm. …
  • GNU Emacs.

What text editor should I use for Linux?

There are two command-line text editors in Linuxxae: vim and nano. You can use one of these two available options should you ever need to write a script, edit a configuration file, create a virtual host, or jot down a quick note for yourself. These are but a few examples of what you can do with these tools.

What is the best text editor to use?

Best text editors in 2021: for Linux, Mac, and Windows coders and programmers

  • Sublime Text.
  • Atom.
  • Visual Studio Code.
  • Espresso.
  • Brackets.
  • Notepad++
  • Vim.
  • BBedit.

What is the best IDE for Linux in 2020?

10 Best IDEs For Linux In 2020!

  • NetBeans.
  • zend Studio.
  • Komodo IDE.
  • Anjuta.
  • MonoDevelop.
  • CodeLite.
  • KDevelop.
  • Geany.

Is VI the best text editor?

Vim is the best text editor/IDE out there. It is the x26quot;editor of choice of old-time Unix hackersx26quot;. Vim is one of the most popular programming editors out there. Itx26#39;s loved by geeks for its speed, extensive feature set, and flexibility.

Which text editor is used in Linux?

A Linux system supports multiple text editors. There are two types of text editors in Linux, which are given below: Command-line text editors such as Vi, nano, pico, and more. GUI text editors such as gedit (for Gnome), Kwrite, and more.

Which is the most common text editor?

The 15 Most Popular Text Editors for Developers

  • UltraEdit.
  • Dreamweaver.
  • Komodo Edit / Komodo IDE.
  • Aptana.
  • PSPad.
  • Vim.
  • TextMate.
  • Notepad++

Bash Vs KSH

Bash Vs KSH

Linux and Unix have various shells. Two kinds of these numerous shells are KSH and BASH.

KSH (The Korn Shell) was developed many years before the BASH. Ksh has associative arrays and handles loop syntax better than bash. Also ksh’s command print is better than bash’s echo command. In other way, ksh does not support history completion, process substitution and rebindable command-line editing.

Bash has more added extension than ksh. Bash has tab completion and easier method to set a prompt in order to display current directory.

Compared to ksh, bash is newer and more popular.

Example of difference ksh and bash in condition test. First bash:

if [ $i -eq 3 ]

and condition test in ksh:

if (($i==3))

Bash can handle exit codes from pipes in a cleaner way. Bash and KSH are both Bourne=compatible shells, they share common functions and features and can be interchangeable to use.

Is ksh same as bash?

KSH and Bash shells are also products of combinations of other shells’ features. Bash and KSH are both Bourne-compatible shells. Since they share common features, they can be used interchangeably.

Is ksh faster than bash?

The ksh and zsh seems about seven times faster than bash . The ksh excelled in 17 tests and the zsh in six tests.

What is the difference between ksh CSH and bash?

CSH is C shell while BASH is Bourne Again shell. 2. C shell and BASH are both Unix and Linux shells. While CSH has its own features, BASH has incorporated the features of other shells including that of CSH with its own features which provides it with more features and makes it the most widely used command processor.

Is ksh a Linux shell?

Ksh is an acronym for KornSHell. It is a shell and programming language that executes commands read from a terminal or a file. It was developed by David Korn at AT&T Bell Laboratories in the early 1980s. It is backwards-compatible with the Bourne shell and includes many features of the C shell.

Why is ksh used?

ksh is a command and programming language that executes commands read from a terminal or a file. rksh is a restricted version of the command interpreter ksh; it is used to set up login names and execution environments whose capabilities are more controlled than those of the standard shell.

How do I run a ksh script in Bash?

How do I run . sh file shell script in Linux?

  • Open the Terminal application on Linux or Unix.
  • Create a new script file with .sh extension using a text editor.
  • Write the script file using nano script-name-here.sh.
  • Set execute permission on your script using chmod command : chmod +x script-name-here.sh.
    To run your script :

Is dash faster than Bash?

If you need speed, go definitely with dash, it is much faster than any other shell and about 4x faster than bash.

What is faster than Bash?

Perl is absurdly faster than Bash. And, for text manipulation, you can actually achieve better performances with Perl than with C, unless you take time to write complex algorithms.

How much faster is dash than Bash?

Dash is not Bash compatible, but Bash tries to be mostly compatible with POSIX, and thus Dash. Dash shines in: Speed of execution. Roughly 4x times faster than Bash and others.

What is difference between sh and ksh in Unix?

sh is the original Bourne shell. On many non-Linux systems, this is an old shell without the POSIX features. Thus bash and ksh (or even csh and tcsh) are better choices than sh. … Public domain ksh (pdksh) is Bourne-compatible and mostly POSIX-compatible.

Is zsh better than Bash?

It has many features like Bash but some features of Zsh make it better and improved than Bash, such as spelling correction, cd automation, better theme, and plugin support, etc. Linux users don’t need to install the Bash shell because it is installed by default with Linux distribution.

Bash How to Print Array

Bash How to Print Array

Arrays are collection of elements, The Arrays in bash are indexed from 0 (zero-based).
Below is the definition on an Array in Bash
my_array=(zero one two three four)

Now our array is defined.
Here is exactly how the my_array is stored on BASH:

my_array=([0]="zero" [1]="one" [2]="two" [3]="three" [4]="four")

You can explicit define an array:

declare -a MY_ARRAY

You can view the declarations along with other environment variables using the declare command.

declare -p my_array
declare -a my_array=([0]="zero" [1]="one" [2]="two" [3]="three" [4]="four")

Now if you try to print the array:

my_array=(zero one two three four)
echo $my_array
zero

By default only the first element value is printed which belongs to the 0 index.

To print the first element of the array using the indexing:

my_array=(zero one two three four)
echo ${my_array[0]}
zero

The change we noticed here is the use of the Curly Braces ‘{}’, its used to refer to the value of an item in the array. The curly braces are required to avoid issues with path name expansion.

To read all elements of the array use the symbols “@” or “*”.

echo ${my_array[@]}
zero one two three four
echo ${my_array[*]}
zero one two three four

The difference between “$@” and “$*” is “$@” expands each element as a separate argument, however “$*” expand to the arguments merged into one argument.

To prove this, print the index elements followed by $@ or $* format.

echo ${my_array[$*0]}
zero
echo ${my_array[$@1]}
one
echo ${my_array[$*2]}
two

Getting the Length of the Array

If you need to get the length of the array uses the symbol “#” before the name of the array:

echo "${#my_array[*]}"
5

How do I print an array in bash?

Print Bash Array

We can use the keyword ‘declare’ with a ‘-p’ option to print all the elements of a Bash Array with all the indexes and details. The syntax to print the Bash Array can be defined as: declare -p ARRAY_NAME.

How do I print in bash?

After typing in this program in your Bash file, you need to save it by pressing Ctrl +S and then close it. In this program, the echo command and the printf command is used to print the output on the console.

How do you print an array element in a new line in Shell?

To print each word on a new line, we need to use the keys “%s’\n”. ‘%s’ is to read the string till the end. At the same time, ‘\n’ moves the words to the next line. To display the content of the array, we will not use the “#” sign.

How do I display all array elements at once?

Program:
  1. public class PrintArray {
  2. public static void main(String[] args) {
  3. //Initialize array.
  4. int [] arr = new int [] {1, 2, 3, 4, 5};
  5. System. out. println(“Elements of given array: “);
  6. //Loop through the array by incrementing value of i.
  7. for (int i = 0; i < arr. length; i++) {
  8. System. out. print(arr[i] + ” “);

How do I create an array in bash?

  1. To declare your array, follow these steps:
    Give your array a name.
  2. Follow that variable name with an equal sign. The equal sign should not have any spaces around it.
  3. Enclose the array in parentheses (not brackets like in JavaScript)
  4. Type your strings using quotes, but with no commas between them.

How do you create an array in bash?

Define An Array in Bash

You have two ways to create a new array in bash script. The first one is to use declare command to define an Array. This command will define an associative array named test_array. In another way, you can simply create Array by assigning elements.

How does printf work in bash?

What Is the Bash printf Function? As the name suggests, printf is a function that prints formatted strings of text. That means you can write a string structure (the format) and later fill it in with values (the arguments). If you’re familiar with the C/C++ programming languages, you might already know how printf works.

How do you pass an array to a function in bash?

10 Answers

  1. Expanding an array without an index only gives the first element, use copyFiles “${array[@]}” instead of copyFiles $array.
  2. Use a she-bang #!/bin/bash.
  3. Use the correct function syntax. Valid variants are function copyFiles {… …
  4. Use the right syntax to get the array parameter arr=(“$@”) instead of arr=”$1″

How do you create an empty array in bash?

To declare an empty array, the simplest method is given here. It contains the keyword “declare” following a constant “-a” and the array name. The name of the array is assigned with empty parenthesis.

How do I get the size of an array in bash?

To get the length of an array, we can use the {#array[@]} syntax in bash. The # in the above syntax calculates the array size, without hash # it just returns all elements in the array.

How to manage LVM in Linux

How to manage LVM in Linux

Creating Dynamic Disc

We are going to create three discs on a USB drive, each disc will have 512 MB. First, verify if we have connected the USB drive as /dev/sdc/:

# fdisk -l

If we have a USB drive connected as /dev/sdc:

# fdisk /dev/sdc

Command d can remove existing logical volumes on the USB drive.

Enter commands n, p, 1-3, +512M to create 3 logical volumes. For example, we create the first volume /dev/sdc1:

Command (m for help): p
Command (m for help): n
Partition number (1-3): 1
First sector (34-41943006, default 41940992): 34
Last sector, +sectors or +size{K,M,G,T,P}: +512M
Command (m for help): p

Command w saves new partition table and changes can not be undone.

Reload partition table:

# partprobe

Format created discs:

# pvcreate /dev/sdc1
# pvcreate /dev/sdc2
# pvcreate /dev/sdc3

Verify:

# pvdisplay

Create volume group DATA:

# vgcreate DATA /dev/sdc1 /dev/sdc2 /dev/sdc3

To be sure verify group capacity:

# vgdisplay

Create physical volume from 100% of physical volume capacity.

# lvcreate -l +100%FREE -n new_volume DATA

Format the new volume:

# mkfs -t ext3 /dev/DATA/new_volume

Create a directory for mount:

# mkdir /mnt/dynamic

Mount the new volume:

# mount /dev/DATA/new_volume /mnt/dynamic

Mount verification:

# df -hT

Mount verification using a graphical interface:

# system-config-lvm

Dynamic Resize Dynamic Disk

Create primary logical partition /dev/sdc4, that use the rest of the USB drive:

# fdisk /dev/sdc
Command (m for help):n
Command (m for help):4
Command (m for help):p
<enter>
<enter>

Command w saves a new partition table and it can not be undone.

Verify partition table:

# partprobe

Add new disk to physical LVL partition:

# pvcreate /dev/sdc4

Verify:

# pvdisplay

Extend volume group:

# vgextend DATA /dev/sdc4

Verify extended volume group:

# vgdisplay

Extend logical volume with 1 GB:

# lvextend -L+1G /dev/DATA/new_volume

Resize size of disk:

# resize2fs /dev/DATA/new_volume

Remount:

# mount -o remount /dev/DATA/new_volume

Verify:

# df -hT

Removing Dynamic Disc

Unmount logical volume:

# umount /dev/DATA/new_volume

Remove logical volume:

# lvremove /dev/DATA/new_volume

Remove volume group:

# vgremove /dev/DATA

Remove physical volumes:

# pvremove /dev/sdc1
# pvremove /dev/sdc2
# pvremove /dev/sdc3
# pvremove /dev/sdc4

Verification using a graphical interface:

# system-config-lvm

How to Make Software RAID in Linux

How to Make Software RAID in Linux

Add a new partition on the first physical disc:

# fdisk /dev/sda

Using the same option add a new partition on the second physical disc:

# fdisk /dev/sdb

Create a new RAID volume md0 type of RAID1 (raid level1):

# mdadm --create /dev/md0 --level=1 --raiddevices=2 /dev/sda7 /dev/sdb2

Check and verify addition:

# watch cat /proc/mdstat

Format the new RAID volume to ext3 filesystem:

# mkfs -t ext3 /dev/md0

Verify created RAID:

# mdadm --query /dev/md0

For more details enter:

# mdadm --query --detail /dev/md0

Create a directory for mounting RAID volume:

# mkdir /mirror

Mount RAID volume md0:

# mount /dev/md0 /mirror

Verify:

# df -hT

Let’s try to create directory /etc and file numbers.txt:

# cp -ar /etc /mirror
# seq 10000000 > /mirror/cisla.txt

Verify:

# ls -ltr /mirror
# tail /mirror/subor.txt

You can anytime monitor volume [UU]:

# cat /proc/mdstat

We need to configure automatic mounting volume md0 after the system starts:

# cat /etc/mtab | grep md0
# joe /etc/fstab

Append next lines at the end of file /etc/fstab:

/dev/md0 /mirror ext3 defaults 0 0

Append next lines at the end of file /etc/mdadm.conf. If any error occurred, the system will send an email about it.

DEVICE /dev/sd[a-z]*
ARRAY /dev/md0 level=raid1 devices=/dev/sda7,/dev/sdb2
MAILADDR root@localhost

Turn the monitoring service on:

# service mdmonitor start

Try to make disc down:

# yes 'We love linux' > /mirror/linux.txt

Caution: yes command will repeatedly output the string until killed.
If you pull out the disc during the yes command you should get an email with the error message:

# mutt

We can see one disc is not available [_U]:

# cat /proc/mdstat

Removing partitions that physically don’t exist:

# mdadm /dev/md0 --detached

Removing partitions that physically exist:

# mdadm --manage /dev/md0 --remove /dev/sdb2

Connect physical disc and monitor how it is being synchronized:

# mdadm --manage /dev/md0 --add /dev/sdb2
# watch cat /proc/mdstat

Test RAID functionality. We should see “active raid1”:

# cat /proc/mdstat

verify RAID:

# mdadm --detail /dev/md0

Disable RAID:

# umount /dev/md0
# mdadm --manage --stop /dev/md0

Test RAID functionality again, we should not see “active raid1”:

# cat /proc/mdstat

RAID: removing

Unmount mounted partition:

# umount /mirror

Stop RAID (to start it again use –assemble –scan):

# mdadm --stop /dev/md0

Remove RAID:

# mdadm --remove /dev/md0

RAID partitions are readable, we can mount it:

# mount /dev/sdb2 /mnt

We should see details about RAID:

# ls -ltrh /mnt

Unmount RAID partition:

# umount /dev/sdb2

Linux How to Execute Script

Linux How to Execute Script

There are many ways how to execute a script, the first one is to execute the script by specifying the interpreter:

bash script.sh
sh script.sh

For debugging use option “-x” to see what is being done.

bash -x script.sh

As an interpreter, you can use sh, ksh, csh, bash etc.

If you want to execute the script without specifying an interpreter, you need to set execute (+x) permission:

chmod +x script.sh

Then you can execute the script following way:

./script.sh

Another way how to run the script:

source script.sh

The script doesn’t need to execute permission in this example. If the script makes any changes to the environment, it will be visible after running the script, because commands in the script are executed in the current shell.

Linux How to Extend Swap Space

Linux How to Extend Swap Space

Display swap usage summary

# swapon -s

Show how many space swap needs in megabytes

# free -m

Create new swap disk.

# fdisk /dev/sdb

Format new disk to swap format

# mkswap /dev/sdb1

Add new part of swap

# swapon /dev/sdb1

Verify if there is more space in swap space

# free -m

Append next line to file /etc/fstab.

/dev/sdb2 swap swap defaults 0 0

If you need old swap use

# swapoff /dev/sda6

Linux How to Change Hostname

Linux How to Change Hostname

Hostname identifies a computer on the network and it is visible in the terminal. We set up hostname during the installation operating system, but sometimes we want to change it.

Use command “hostname” to change hostname:

hostname new_hostname

Will set the hostname of the system to new_hostname. Open a new terminal to verify change hostname.

If you want to change the hostname permanent use the command “sysctl kernel.hostname” to change it:

sysctl kernel.hostname=NewHostname

Output: kernel.hostname = Newhostname

Command hostname without any parameter prints the current hostname of the system.

Linux How to Generate Random Password

Linux How to Generate Random Password

Generating password is relatively simple and can be done in different ways. You can install tools to generate passwords, a few examples:

  • mkpasswd (Debian/Ubuntu)
  • makepasswd (Debian/Ubuntu)
  • pwgen (CentOS/Fedora/RHEL)

When you don’t have installed these tools, here are useful commands:

date +%s | sha256sum | base64 | head -c 32 ; echo

We used time in seconds as a input to hash function sha-256 and print first 32 chars. You can replate sha256sum with other hash function (md5sum).

strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 32 | tr -d '\n'; echo

/dev/urandom is the built-in feature which generate random chars.

< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32; echo

Another way using /dev/urandom, even simpler.

You can also create the script:

generatePasswd () {
local l=$1
[ "$l" == "" ] && l=16
tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs
}

generatePasswd "$1"

Bash How to Return from Function

Bash How to Return from Function

Use the statement “return” to return from the function. You can also specify the return value. Example:

return 1234

We returned function status 1234 from function. Usually, we use 0 value for success and 1 for failure. It is similar to command exit which we use to terminate the script.

If you don’t use the return statement in the whole function, the status of the last executed command will be returned.

To verify returned status from the last called function use “$?”.

There is a difference between commands return and exit. The exit will cause the script to end at the line where it is called. Return will cause the current function to go out of scope and continue execution command after the function.

Bash How to Read from Keyboard

Bash How to Read from Keyboard

To read input from the keyboard and assign input value to a variable use the read command.

Read syntax:

read options var1 var2

To write the value of first entered word use:

read var1 var2
echo $var1

If you don’t give any argument to the read command, input will assign to the environment variable REPLY.

The “s” option does not echo input while reading from a keyboard.

read -s -p "Enter password:" $password

The -p “TEXT” option displays TEXT to the user without a newline.

The -e option means that command readline is used to obtain the line.
Option -u FD reads inputs from file descriptor FD (0,1,2).
Option -t TIME causes that read returns a failure if the input is not read within TIME seconds.