How to Copy and Paste in Bash [2 Methods with Cases]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

In Bash, copy and paste typically refers to operations involving text or files within the command line interface or the terminal environment. It is an essential technique to increase productivity in every application. In Bash, you can copy and paste text in the terminal or in the system’s built-in editor, enabling fast and efficient information transfer.

In this article, I will show you how to perform the copy-and-paste operation in Bash.

2 Methods to Copy and Paste Within Bash Script in Nano

Copy and paste in Bash script differs from regular copy and paste in various documents of the Windows operating system. Here I am going to demonstrate two different ways to copy and paste within Bash script in nano.

1. Keyboard Shortcut to Copy-Paste Within Bash Script

Copying and pasting codes within a Bash script saves a significant amount of time in writing codes. For scripting, Nano is one of the popular built-in editors in the Unix operating system. Programmers often write codes in nano. It offers some keyboard shortcuts including shortcuts for copying and pasting. Follow these steps to perform copy-paste operations within a script in the nano editor:

  1. First, launch an Ubuntu Terminal.
  2. Write the following command to open a cp_paste.sh file in the build-in nano editor:
    nano cp_paste.sh

    Creating a .sh file in nano to copy paste in bash

  3. Now, write the script below. At some point, I want to copy the variable current_date later in the script. First, select the variable using CTRL+SHIFT+LEFT ARROW / RIGHT ARROW keyboard combination.
    #!/bin/bash
    
    read -p "What is your name? " name
    
    echo "Hello, $name! Welcome to the Bash script."
    current_date=$(date +"%Y-%m-%d")
    echo "The current date is: $current_date"

    Selecting desired text in nano to copy and pasteHere, the current_date variable is selected. Now you can copy the selected text.

  4. After selection, press the CTRL+6 to copy the selected text.
    Note: Make sure you press 6 from the main keyboard and not from the numeric keypad.
  5. After copying the text, place the cursor where to paste the text. Then use CTRL+U keyboard shortcuts to paste the text.Taking cursor in the position to paste The cursor is placed in the position to paste the selected text. Pasting the copied text in the desired location The selected text “current_date” is pasted at the desired position.

2. Copy and Paste by Using Right Click

You can copy the selected text and paste it somewhere in the script in Nano using right-click. This method is efficient for copying and pasting into and out of the nano editor. It means you can copy text from other documents or applications and still paste it into Nano. Follow the steps to copy and paste using right-click:

  1. First, click and hold the left mouse button and drag the cursor to select the desired text. The selected text will be highlighted as you drag the cursor. In the script, I want to copy the variable current_date.
  2. Now, release the left mouse button to complete the selection.
  3. Right-click on the mouse and select copy.
  4. Place the cursor in the position to paste the text.
  5. Finally, right-click and select paste to paste the copied text in the desired location.Pasting text using right click in nano

How to Copy Paste Files and Directories?

Copying files and directories is one of the most important features of every operating system. Use the simple Bash script below to copy files and directories in the Unix operating system:

#!/bin/bash

# Specify source file and destination file
source_file=/home/laku/mailx.sh

destination_file=/usr/local/bin
# Copy the files to the destination directory
sudo cp $source_file $destination_file
EXPLANATION

This bash script is designed to copy a specific file (mailx.sh) from a defined source path (/home/laku/) to a specified destination directory (/usr/local/bin). The script uses the cp command to perform the copy operation with sudo to ensure that the file can be copied to a protected directory.

Note: Make the file executable before running it using:

chmod u+x copy_files.sh

Changing permission to copy files in Bash

Finally, run the script:Execute the script to copy files When executed the program will copy the mailx.sh file to the protected /usr/local/bin folder.

How to Copy Paste Text in Linux Terminal?

To copy and paste in the Linux terminal or command prompt of Bash Shell, use CTRL+SHIFT+C and CTRL+SHIFT+V. Otherwise, right-click with the mouse and select Copy and Paste from the pop-up menu.

To copy & paste text in the Linux terminal, go through the below steps:

  1. First, click and hold the left mouse button and drag the cursor to select the desired text in the terminal. The selected text will be highlighted as you drag the cursor.
  2. Copy the selected text using CTRL+SHIFT+C.Keyboard shortcut to copy in the terminal Once you copy a text from the terminal, you can further paste the text no matter whether in the terminal or outside of the terminal.
  3. Now to paste, first, copy a command in the clipboard (for example, the command below):
    echo "deb https://download.sublimetext.com/ apt/stable/" | sudo tee /etc/apt/sources.list.d/sublime-text.list
  4. Next. open the system terminal.
  5. Finally, paste the command by pressing CTRL+SHIFT+V. Hopefully, the command is pasted in the terminal.Pasting text in the terminal

Apart from this keyboard shortcut, you can copy and paste selected text within the system terminal using right-click.

Conclusion

In conclusion, copy paste in Bash is quite easy and simple. I hope you can now perform copy-paste within a Bash script in Nano as well as in the system terminal.

People Also Ask

How do I copy and paste in Linux?

To copy text from the Linux terminal, select the text you want to copy using the mouse or keyboard arrow keys, then press CTRL+SHITF+C. To paste into the terminal, position the cursor where you want to paste the text, then press CTRL+SHIFT+V.

Is there any copy-paste command in Linux?

NO. There is no direct command for the purpose of copy-paste in Bash. However, Linux offers copy-paste functionality by means of keyboard shortcuts and a right-click menu.

Is CTRL+C to copy works in Linux terminal?

NO. To copy in a Linux terminal use CTRL+SHIFT+C and to paste use CTRL+SHIFT+V.

How to copy paste in Linux vi editor?

To copy-paste inside the Linux vi editor, press y to yank or copy the selected text and press p to paste the copied text.

How do I copy and paste in Linux?

To copy text from the Linux terminal, select the text you want to copy using the mouse or keyboard arrow keys, then press CTRL+SHITF+C. To paste into the terminal, position the cursor where you want to paste the text, then press CTRL+SHIFT+V.

Is there any copy-paste command in Linux?

NO. There is no direct command for the purpose of copy-paste in Bash. However, Linux offers copy-paste functionality by means of keyboard shortcuts and a right-click menu.

Is CTRL+C to copy works in Linux terminal?

NO. To copy in a Linux terminal use CTRL+SHIFT+C and to paste use CTRL+SHIFT+V.

How to copy paste in Linux vi editor?

To copy-paste inside the Linux vi editor, press y to yank or copy the selected text and press p to paste the copied text.

Related Articles


<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial

Rate this post
Md Zahidul Islam Laku

Hey, I'm Zahidul Islam Laku currently working as a Linux Content Developer Executive at SOFTEKO. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). I write articles on a variety of tech topics including Linux. Learning and writing on Linux is nothing but fun as it gives me more power on my machine. What can be more efficient than interacting with the Operating System without Graphical User Interface! Read Full Bio

Leave a Comment