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

Copy-paste is essential 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.

Key Takeaways

  • Copy and Paste text.
  • Copy and Paste files and directories.
  • Copy and Paste in Terminal.

Free Downloads

2 Methods to Copy and Paste into Bash Script

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.

You can read the Comparative Analysis of Methods to find one that suits you.

Method 1: Keyboard Shortcut for Copy-paste Within Bash Script

Copying and pasting codes within a Bash script saves a significant amount of time in writing codes. Nano is one of the popular built-in editors in Unix operating system. Programmers often write codes in nano. It offers some keyboard shortcuts including shortcuts for copying and pasting. Here, I am going to show you how to perform copy-paste operations within a script in nano editor.

Steps to Follow >

❶ At first, launch an Ubuntu Terminal.

❷ Write the following command to open a cp_paste.sh file in the build-in nano editor:

nano cp_paste.sh
EXPLANATION
  • nano: Opens a file in the Nano text editor.
  • cp_paste.sh: Name of the file.
Creating a .sh file in nano to copy paste in bash❸ 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, current_date variable is selected. Now you can copy the selected text.

➍ 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.

➎ 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 pasteCursor is placed at the position to paste the selected text. Pasting the copied text in the desired locationThe selected text “current_date” is pasted at the desired position.

Method 2: Copy and Paste by Using Right Click

Using right-click one can copy selected text and paste it somewhere in the script in nano. This method is efficient for copying and pasting into and out of nano editor. It means you can copy text from other document or application and still paste it into nano. Here, I am demonstrating the same task in the same script using right-click.

You can follow the Steps of Method 1 to learn about creating and saving shell scripts.

Steps to Follow >

❶ 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.

❷ Now, release the left mouse button to complete the selection.

❸ Right-click on the mouse and select copy.

➍ Place the cursor in the position to paste the text.

➎ Finally, right-click and select paste to paste the copied text in the desired location.

Pasting text using right click in nano

Comparative Analysis of Methods

Here is a comparative analysis between the above-discussed methods. Take a look at the analysis to determine which method suits your need.

Method Advantage Disadvantage
Keyboard Shortcut
  • Quick and fast.
  • Works only within

nano.

Right click
  • Efficient in copying and pasting text into and out of nano.
  • User-friendly.
  • Slow compared to

keyboard Shortcut method.

In my case, I prefer using the right-click option for copying and pasting. Nevertheless, for those with a strong memory, I would recommend utilizing keyboard shortcuts.

2 Cases of Copying and Pasting in Bash

Copying and pasting files and directories is important. Knowing how to copy and paste within the system terminal is also important. These two are entirely different cases. In the subsequent demonstrations, I will show you how to perform both.

Case 1: How to Copy and 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 Unix operating system.

You can follow the Steps of Method 1 to learn about creating and saving shell scripts.

Scripts (copy_files.sh) >

#!/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.

At First, use the following command to make the file executable:

chmod u+x copy_files.sh
EXPLANATION
  • chmod: Changes permissions.
  • u+x: Giving the owner executing permission.
  • copy_files.sh: Name of the script.
Changing permission to copy files in BashFinally, run the script by the following command:
./copy_files.sh

Execute the script to copy filesWhen executed the program will copy the mailx.sh file to the protected /usr/local/bin folder.

Case 2: How to Copy and Paste Text in Linux Terminal

The user frequently needs to copy and paste stuff into the terminal in order to install an application or learn more about an error message. The keyboard shortcut is the most effective technique to copy data and paste commands in the system terminal.

Steps to Copy >

➊ 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.

❷ 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.

Steps to Paste >

➊ 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

❷ Open the system terminal.

❸ 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. Hope you can now perform copy-paste within a Bash script in nano as well as in the system terminal.

People Also Ask

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 the copy-paste functionality by means of keyboard shortcuts and right-click menu.
Is CTRL+C to copy works in Linux terminal?
NO. To copy in Linux terminal use CTRL+SHIFT+C and to paste use CTRL+SHIFT+V
How to copy paste in Linux vi editor?
Press y to yank or copy the selected text and press p to paste the copied text in vi editor.

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