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.
- Copy and Paste text.
- Copy and Paste files and directories.
- Copy and Paste in Terminal.
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.
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.
❶ At first, launch an Ubuntu Terminal.
❷ Write the following command to open a cp_paste.sh file in the build-in nano editor:
❸ 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"
Here, current_date variable is selected. Now you can copy the selected text.
➍ After selection, press the CTRL+6 to copy the selected text.
➎ After copying the text, place the cursor where to paste the text. Then use CTRL+U keyboard shortcuts to paste the text.Cursor is placed at the position to paste the selected text.The 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.
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.
Here is a comparative analysis between the above-discussed methods. Take a look at the analysis to determine which method suits your need.
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.
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
At First, use the following command to make the file executable:
Finally, run the script by the following command:
chmod u+x copy_files.sh
When 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.
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.
Apart from this keyboard shortcut, you can copy and paste selected text within the system terminal using right-click.
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
- How to Get Date in Bash [2 Methods with Examples]
- How to Print Time in Bash [2 Quick Methods]
- How to List Users in Bash [2 Easy Ways]
- How to Get Current Time in Bash [4 Practical Cases]
- How to Use Date Format in Bash [5 Examples]
- How to Get Timestamp in Bash [2 Practical Cases]
- How to Read Password in Bash [3 Practical Cases]
- How to Send Email in Bash [2 Easy Methods]
- Bash Script to Send Email with Attachment [Step-by-Step Guide]
- How to Get IP Address in Bash [3 Methods]
- How to Find and Replace String in Bash [5 Methods]
- How to Get Script Name Using Bash Script? [3 Easy Ways]
- How to Call Another Script in Bash [2 Methods]
- How to Generate UUID in Bash [3 Simple Methods]
- 3 Easy Ways to Write to a File in Bash Script
- How to Write the Output to a File in Bash Script [5 Practical Cases]
- How to Create a List in Bash Scripts? [2 Easy Methods]
- How to Clear History in Bash [2 Practical Cases]
- How to Clear Screen Using Bash Script? [2 Effective Methods]
- How to Check Ubuntu Version Using Bash Scripts? [2 Methods]