<!DOCTYPE html>
    <html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
    <head>
<title>Cách sử dụng lệnh read trong Linux</title>
<meta name="description" content="Cách sử dụng lệnh read trong Linux - Savefile - Tin Tức -...">
<meta name="author" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :.">
<meta name="copyright" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :. [admin@nguoicodonvn2008.info]">
<meta name="robots" content="index, archive, follow, noodp">
<meta name="googlebot" content="index,archive,follow,noodp">
<meta name="msnbot" content="all,index,follow">
<meta name="generator" content="NukeViet v4.5">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta property="og:title" content="Cách sử dụng lệnh read trong Linux">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;www.nguoicodonvn2008.info&#x002F;vi&#x002F;news&#x002F;savefile&#x002F;kien-thuc-may-tinh&#x002F;cach-su-dung-lenh-read-trong-linux-9407.html">
<meta property="og:site_name" content=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.">
<meta property="og:url" content="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/cach-su-dung-lenh-read-trong-linux-9407.html">
<link rel="shortcut icon" href="https://nguoicodonvn2008.info/favicon.ico">
<link rel="canonical" href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/cach-su-dung-lenh-read-trong-linux-9407.html">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/karaoke-dual/" title="Tin Tức - Karaoke Dual" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/nhac-tre/" title="Tin Tức - Nhạc trẻ" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/tru-tinh/" title="Tin Tức - Trữ tình" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/nuoc-ngoai/" title="Tin Tức - Nước ngoài" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/remix/" title="Tin Tức - Remix" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/tam-su-tinh-yeu/" title="Tin Tức - Tâm sự tình yêu" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/tho-suu-tam/" title="Tin Tức - Thơ sưu tầm" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/cuoc-song/" title="Tin Tức - Cuộc sống" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/phan-mem/" title="Tin Tức - Phần mềm" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/kien-thuc-may-tinh/" title="Tin Tức - Kiến thức máy tính" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/hoc-tap/" title="Tin Tức - Học tập" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/tai-lieu/" title="Tin Tức - Tài liệu" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/de-thi/" title="Tin Tức - Đề thi" type="application/rss+xml">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/assets/css/font-awesome.min.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/style.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/style.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/news.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/custom.css" type="text/css">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/jquery/jquery.min.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/language/vi.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/global.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/site.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/news.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/main.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/custom.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/bootstrap.min.js" type="text/javascript">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/assets/css/font-awesome.min.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/style.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://nguoicodonvn2008.info/themes/default/css/news.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/custom.css">
<style type="text/css">
	body{background: #fff;}
</style>
    </head>
    <body>
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">.&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.</h2>
		<p class="pull-right"><a title=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;." href="https://nguoicodonvn2008.info/">https://nguoicodonvn2008.info</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>Cách sử dụng lệnh read trong Linux</h1>
		<ul class="list-inline">
			<li>Thứ năm - 21/11/2024 22:58</li>
			<li class="hidden-print txtrequired"><em class="fa fa-print">&nbsp;</em><a title="In ra" href="javascript:;" onclick="window.print()">In ra</a></li>
			<li class="hidden-print txtrequired"><em class="fa fa-power-off">&nbsp;</em><a title="Đóng cửa sổ này" href="javascript:;" onclick="window.close()">Đóng cửa sổ này</a></li>
		</ul>
		<div class="clear"></div>
		<div id="hometext">
		</div>
				<div class="imghome">
			<img alt="Cách sử dụng lệnh read trong Linux" src="https://st.quantrimang.com/photos/image/2024/09/17/lenh-read-trong-linux-3.jpg" width="460" class="img-thumbnail" />
		</div>
		<div class="clear"></div>
		<div id="bodytext" class="clearfix">
			<p style="text-align: justify;">Đôi khi khi tương tác với hệ thống Linux, bạn có thể cần nhắc người dùng nhập dữ liệu hoặc đọc dữ liệu từ các file hoặc thậm chí muốn đặt thời gian chờ. Bạn có thể thực hiện các tác vụ này và nhiều tác vụ khác bằng lệnh read và các tùy chọn khác nhau của lệnh này.</p>

<p style="text-align: justify;">Bài viết này sẽ hướng dẫn bạn những điều cơ bản về lệnh read và các tùy chọn của lệnh này thông qua một số ví dụ.</p>

<h2 style="text-align: justify;">&nbsp;</h2>

<h2 id="mcetoc_1id74q8iq171" style="text-align: justify;">Lệnh Read là gì?</h2>

<p style="text-align: justify;">Trong Linux, bạn có thể sử dụng lệnh read để ghi lại dữ liệu đầu vào của người dùng hoặc đọc một dòng từ đầu vào chuẩn (stdin). Lệnh này đọc tổng số byte từ mô tả file đã cho và lưu trữ chúng trong buffer. Sau đó, lệnh trả về số byte đã đọc, số không hoặc lỗi.</p>

<p style="text-align: justify;">Ví dụ, nếu số hoặc số đếm bằng không, lệnh sẽ tham chiếu đến phần cuối của file. Nhưng khi thành công, lệnh sẽ trả về số byte đã đọc. Nếu lệnh read tìm thấy một số lỗi, lệnh sẽ trả về -1.</p>

<p style="text-align: justify;">Trước khi khám phá các tùy chọn lệnh read, trước tiên chúng ta hãy xem cú pháp của lệnh read:</p>

<pre id="pre0">
<code>read &#91;options&#93; &#91;name…&#93;</code></pre>

<p style="text-align: justify;">Ở đây, tham số&nbsp;<strong>options</strong>&nbsp;chỉ định nhiều flag khác nhau được sử dụng để sửa đổi hành vi của lệnh read. Ngoài ra, tham số&nbsp;<strong>name</strong>&nbsp;chỉ định tên của nhiều biến được sử dụng để lưu trữ dữ liệu đầu vào. Nếu không cung cấp tên, dữ liệu đầu vào sẽ được giữ lại trong biến bash&nbsp;<strong>$REPLY.</strong></p>

<div style="text-align: justify;">&nbsp;</div>

<h2 id="mcetoc_1id74q8iq172" style="text-align: justify;">Các tùy chọn lệnh read</h2>

<p style="text-align: justify;">Lệnh read của Bash có nhiều tùy chọn để kiểm soát dữ liệu đầu vào của người dùng. Một số tùy chọn không cần tham số bổ sung, trong khi một số khác thì cần.</p>

<p style="text-align: justify;">Hãy cùng khám phá một số tùy chọn mà chúng ta có thể sử dụng với lệnh read:</p>

<table border="1">
		<tr>
			<th style="text-align: justify;">Tùy chọn</th>
			<th style="text-align: justify;">Mô tả</th>
		</tr>
	<tbody>
		<tr>
			<td style="text-align: justify;"><strong>-a &lt;array&gt;</strong></td>
			<td style="text-align: justify;">Nó lưu trữ dữ liệu đầu vào dưới dạng một mảng thay vì các biến riêng biệt.</td>
		</tr>
		<tr>
			<td style="text-align: justify;"><strong>-s</strong></td>
			<td style="text-align: justify;">Chạy âm thầm, nghĩa là dữ liệu đầu vào không được hiển thị trên terminal</td>
		</tr>
		<tr>
			<td style="text-align: justify;"><strong>-e</strong></td>
			<td style="text-align: justify;">Cho phép hỗ trợ thư viện readline và đọc dòng input</td>
		</tr>
		<tr>
			<td style="text-align: justify;"><strong>-i &lt;prefix&gt;</strong></td>
			<td style="text-align: justify;">Cung cấp giá trị đầu vào ban đầu xuất hiện tại dấu nhắc khi sử dụng readline</td>
		</tr>
		<tr>
			<td style="text-align: justify;"><strong>-p &lt;prompt&gt;</strong></td>
			<td style="text-align: justify;">Hiển thị lời nhắc được chỉ định trước khi đọc đầu vào</td>
		</tr>
		<tr>
			<td style="text-align: justify;"><strong>-u &lt;file descriptor&gt;</strong></td>
			<td style="text-align: justify;">Đọc từ một mô tả file được chỉ định thay vì đầu vào chuẩn (stdin)</td>
		</tr>
		<tr>
			<td style="text-align: justify;"><strong>-d &lt;delimiter&gt;</strong></td>
			<td style="text-align: justify;">Cho phép chỉ định một dấu phân cách dòng đầu vào thay vì ký tự xuống dòng mặc định</td>
		</tr>
		<tr>
			<td style="text-align: justify;"><strong>-t &lt;time&gt;</strong></td>
			<td style="text-align: justify;">Đặt thời gian chờ cho đầu vào; nếu đầu vào không được nhận trong thời gian này, lệnh read sẽ trả về lỗi</td>
		</tr>
		<tr>
			<td style="text-align: justify;"><strong>-r</strong></td>
			<td style="text-align: justify;">Khi được thiết lập, dấu gạch chéo ngược không được coi là ký tự thoát</td>
		</tr>
		<tr>
			<td style="text-align: justify;"><strong>-n &lt;number&gt;</strong></td>
			<td style="text-align: justify;">Chỉ đọc số lượng ký tự được chỉ định</td>
		</tr>
	</tbody>
</table>

<p style="text-align: justify;">Nhập lệnh sau để xuất menu trợ giúp lệnh read:</p>

<pre id="pre1">
<code>head --help</code></pre>

<h2 id="mcetoc_1id74q8iq173" style="text-align: justify;">Cách đọc đầu vào bằng lệnh read</h2>

<p style="text-align: justify;">Cách đơn giản nhất để sử dụng lệnh read là sử dụng lệnh này mà không có bất kỳ đối số hoặc tùy chọn nào. Khi chỉ thực thi lệnh read, lệnh sẽ yêu cầu bạn cung cấp đầu vào mà bạn muốn đọc. Sau khi cung cấp đầu vào, lệnh sẽ thoát và lưu trữ đầu vào đó trong biến mặc định có tên là REPLY.</p>

<p style="text-align: justify;">Hãy lấy ví dụ sau:</p>

<pre id="pre2">
<code>read
&lt;user_input&gt;</code></pre>

<figure>
<div style="text-align: justify;"><img alt="Sử dụng lệnh read để thu thập đầu vào từ người dùng" data-i="0" data-src="https://st.quantrimang.com/photos/image/2024/07/31/lenh-read-trong-linux-1.jpg" data-was-processed="true" height="123" src="https://st.quantrimang.com/photos/image/2024/07/31/lenh-read-trong-linux-1.jpg" width="728" /></div>

<figcaption>
<div style="text-align: justify;">Sử dụng lệnh read để thu thập đầu vào từ người dùng</div>
</figcaption>
</figure>

<p style="text-align: justify;">Bây giờ, sau khi cung cấp đầu vào, hãy hiển thị đầu vào đó bằng&nbsp;<a data-type="internal" href="https://quantrimang.com/cong-nghe/cach-su-dung-lenh-echo-trong-linux-166943" title="Cách sử dụng lệnh &#039;echo&#039; trong Linux">lệnh echo</a>:</p>

<pre id="pre3">
<code>echo $REPLY</code></pre>

<figure>
<div style="text-align: justify;"><img alt="Sử dụng lệnh echo để hiển thị đầu ra của biến REPLY" data-i="1" data-src="https://st.quantrimang.com/photos/image/2024/07/31/lenh-read-trong-linux-2.jpg" data-was-processed="true" height="101" src="https://st.quantrimang.com/photos/image/2024/07/31/lenh-read-trong-linux-2.jpg" width="620" /></div>

<figcaption>
<div style="text-align: justify;">Sử dụng lệnh echo để hiển thị đầu ra của biến REPLY</div>
</figcaption>
</figure>

<div style="text-align: justify;">&nbsp;</div>

<p style="text-align: justify;">Trong khi đọc giá trị đầu vào, bạn cũng có thể lưu trữ nó trong bất kỳ biến cụ thể nào khác. Ví dụ, để lưu trữ kết quả vào một biến, hãy nhập lệnh read theo sau là tên biến:</p>

<pre id="pre4">
<code>read variable1
&lt;user_input&gt;</code></pre>

<figure>
<div style="text-align: justify;"><img alt="Đọc giá trị đầu vào của người dùng và lưu trữ vào biến " data-i="2" data-src="https://st.quantrimang.com/photos/image/2024/07/31/lenh-read-trong-linux-3.jpg" data-was-processed="true" height="105" src="https://st.quantrimang.com/photos/image/2024/07/31/lenh-read-trong-linux-3.jpg" width="640" /></div>

<figcaption>
<div style="text-align: justify;">Đọc giá trị đầu vào của người dùng và lưu trữ vào biến</div>
</figcaption>
</figure>

<p style="text-align: justify;">Bây giờ, để hiển thị kết quả, bạn cần sử dụng lệnh echo với biến lưu trữ giá trị:</p>

<pre id="pre5">
<code>echo $variable1</code></pre>

<figure>
<div style="text-align: justify;"><img alt="Hiển thị giá trị đã thu thập của biến bằng lệnh echo" data-i="3" data-src="https://st.quantrimang.com/photos/image/2024/07/31/lenh-read-trong-linux-4.jpg" data-was-processed="true" height="101" src="https://st.quantrimang.com/photos/image/2024/07/31/lenh-read-trong-linux-4.jpg" width="635" /></div>

<figcaption>
<div style="text-align: justify;">Hiển thị giá trị đã thu thập của biến bằng lệnh echo</div>
</figcaption>
</figure>

<h3 id="mcetoc_1id74q8iq174" style="text-align: justify;">Đọc nhiều giá trị</h3>

<p style="text-align: justify;">Không có cách trực tiếp nào để đọc nhiều giá trị bằng lệnh read. Tuy nhiên, bạn có thể chia một câu đầu vào thành nhiều từ và lưu trữ chúng trong các biến khác nhau.</p>

<p style="text-align: justify;">Hãy xem xét ví dụ sau:</p>

<pre id="pre6">
<code>read variable1 variable2 variable3
&lt;user_input&gt;</code></pre>

<figure>
<div style="text-align: justify;"><img alt="Đọc nhiều giá trị và lưu trữ chúng vào nhiều biến" data-i="4" data-src="https://st.quantrimang.com/photos/image/2024/07/31/lenh-read-trong-linux-5.jpg" data-was-processed="true" height="105" src="https://st.quantrimang.com/photos/image/2024/07/31/lenh-read-trong-linux-5.jpg" width="706" /></div>

<figcaption>
<div style="text-align: justify;">Đọc nhiều giá trị và lưu trữ chúng vào nhiều biến</div>
</figcaption>
</figure>

<p style="text-align: justify;">Ở đây, bạn lưu trữ từ đầu tiên của câu trong biến đầu tiên, từ thứ hai trong biến thứ hai và tất cả các từ còn lại trong biến cuối cùng được cung cấp.</p>

<p style="text-align: justify;">Hãy trả về đầu ra bằng lệnh sau:</p>

<pre id="pre7">
<code>echo &lt;$variabl_namee&gt;</code></pre>

<figure>
<div style="text-align: justify;"><img alt="Hiển thị nhiều giá trị bằng lệnh echo" data-i="5" data-src="https://st.quantrimang.com/photos/image/2024/07/31/lenh-read-trong-linux-6.jpg" data-was-processed="true" height="229" src="https://st.quantrimang.com/photos/image/2024/07/31/lenh-read-trong-linux-6.jpg" width="658" /></div>

<figcaption>
<div style="text-align: justify;">Hiển thị nhiều giá trị bằng lệnh echo</div>
</figcaption>
</figure>

<h3 id="mcetoc_1id74q8ir175" style="text-align: justify;">Đọc từ một file</h3>

<p style="text-align: justify;">Mặc dù lệnh read chủ yếu dành cho đầu vào của người dùng, bạn cũng có thể sử dụng lệnh này để đọc các dòng từ file. Để thực hiện việc này, chỉ cần sử dụng loop while, lệnh echo và lệnh read theo sau là tên biến cụ thể:</p>

<pre id="pre8">
<code>while read line; do
echo &quot;$line&quot;
done &lt; samplefile.txt</code></pre>

<figure>
<div style="text-align: justify;"><img alt="Đọc từ file bằng lệnh read" data-i="6" data-src="https://st.quantrimang.com/photos/image/2024/07/31/lenh-read-trong-linux-7.jpg" data-was-processed="true" height="265" src="https://st.quantrimang.com/photos/image/2024/07/31/lenh-read-trong-linux-7.jpg" width="655" /></div>

<figcaption>
<div style="text-align: justify;">Đọc từ file bằng lệnh read</div>
</figcaption>
</figure>

<p style="text-align: justify;">Ở đây, loop while đọc từng dòng của &quot;samplefile.txt&quot; và ghi lại trong biến line. Sau khi đọc tất cả các dòng của file, lệnh echo sẽ hiển thị giá trị của dòng đó.</p>

<div style="text-align: justify;">&nbsp;</div>

<h3 id="mcetoc_1id74q8ir176" style="text-align: justify;">Đọc đầu vào trong loop</h3>

<p style="text-align: justify;">Bạn cũng có thể nắm bắt đầu vào của người dùng theo trình tự lặp lại bằng cách sử dụng lệnh read với loop while. Điều này hữu ích khi bạn muốn thu thập nhiều đầu vào hoặc tiếp tục cho đến khi đáp ứng được một điều kiện cụ thể.</p>

<p style="text-align: justify;">Ví dụ, hãy đọc nhiều đầu vào và cũng hiển thị chúng trên terminal:</p>

<pre id="pre9">
<code>while read line; do
echo &quot;Line: $line&quot;
done</code></pre>

<figure>
<div style="text-align: justify;"><img alt="Đọc giá trị đầu vào từ người dùng trong loop liên tục" data-i="7" data-src="https://st.quantrimang.com/photos/image/2024/07/31/lenh-read-trong-linux-8.jpg" data-was-processed="true" height="294" src="https://st.quantrimang.com/photos/image/2024/07/31/lenh-read-trong-linux-8.jpg" width="672" /></div>

<figcaption>
<div style="text-align: justify;">Đọc giá trị đầu vào từ người dùng trong loop liên tục</div>
</figcaption>
</figure>

<p style="text-align: justify;">Hơn nữa, loop tiếp tục cho đến khi nhận được tín hiệu kết thúc file (EOF), thường là bằng cách nhấn&nbsp;<strong>Ctrl + D</strong>.</p>

<h2 id="mcetoc_1id74q8ir177" style="text-align: justify;">Cách nhắc nhập dữ liệu bằng lệnh Read</h2>

<p style="text-align: justify;">Bạn cũng có thể tạo lời nhắc tương tác sẽ hiển thị trước khi người dùng nhập dữ liệu. Bạn có thể thực hiện việc này bằng cách sử dụng tùy chọn&nbsp;<strong>-p</strong>&nbsp;cùng với lệnh read.</p>

<p style="text-align: justify;">Hãy hiển thị lời nhắc tùy chỉnh và cũng ghi lại đầu vào:</p>

<pre id="pre10">
<code>read -p &quot;Enter your Name: &quot; name</code></pre>

<figure>
<div style="text-align: justify;"><img alt="Tạo lời nhắc trước khi đọc dữ liệu đầu vào từ người dùng" data-i="8" data-src="https://st.quantrimang.com/photos/image/2024/09/17/lenh-read-trong-linux-1.jpg" data-was-processed="true" height="88" src="https://st.quantrimang.com/photos/image/2024/09/17/lenh-read-trong-linux-1.jpg" width="704" /></div>

<figcaption>
<div style="text-align: justify;">Tạo lời nhắc trước khi đọc dữ liệu đầu vào từ người dùng</div>
</figcaption>
</figure>

<h2 id="mcetoc_1id74q8ir178" style="text-align: justify;">Giới hạn đầu vào của người dùng trong khi đọc</h2>

<p style="text-align: justify;">Bạn muốn kiểm soát những gì người dùng có thể nhập? Sử dụng tùy chọn&nbsp;<strong>-n</strong>&nbsp;với lệnh read để chỉ định độ dài ký tự mong muốn của bạn. Ví dụ, nếu bạn muốn ghi lại 4 ký tự đầu tiên của đầu vào người dùng, hãy sử dụng lệnh bên dưới:</p>

<pre id="pre11">
<code>read -n 9 variable</code></pre>

<figure>
<div style="text-align: justify;"><img alt="Đọc đầu vào dựa trên giá trị ký tự đã chỉ định" data-i="9" data-src="https://st.quantrimang.com/photos/image/2024/09/17/lenh-read-trong-linux-2.jpg" data-was-processed="true" height="81" src="https://st.quantrimang.com/photos/image/2024/09/17/lenh-read-trong-linux-2.jpg" width="583" /></div>

<figcaption>
<div style="text-align: justify;">Đọc đầu vào dựa trên giá trị ký tự đã chỉ định</div>
</figcaption>
</figure>

<p style="text-align: justify;">Sau khi thực hiện lệnh trên, bạn chỉ có thể nhập 9 ký tự. Giới hạn đầu vào của người dùng trong khi đọc sẽ loại bỏ nhu cầu sử dụng dấu phân cách và cũng không chia đầu vào thành các từ.</p>

<h2 id="mcetoc_1id74q8ir179" style="text-align: justify;">Phân tách các trường đầu ra bằng IFS</h2>

<p style="text-align: justify;">Bạn có thể sử dụng biến Internal Field Separator (IFS) với lệnh read để xác định cách chia đầu vào thành các trường riêng biệt. Theo mặc định, IFS sử dụng các ký tự khoảng trắng (tab, xuống dòng và khoảng trắng) làm dấu phân cách trường. Tuy nhiên, bạn có thể tùy chỉnh IFS để phù hợp với nhu cầu của mình.</p>

<p style="text-align: justify;">Ví dụ, bạn có thể chỉ định giá trị dấu hai chấm (:) làm dấu phân cách:</p>

<pre id="pre12">
<code>IFS=&#039;:&#039; read name password
&lt;user_input&gt;
echo &quot;Name: $name, Password: $password&quot;</code></pre>

<figure>
<div style="text-align: justify;"><img alt="Đọc lệnh với dấu phân cách Ifs" data-i="10" data-src="https://st.quantrimang.com/photos/image/2024/09/17/lenh-read-trong-linux-3.jpg" data-was-processed="true" height="167" src="https://st.quantrimang.com/photos/image/2024/09/17/lenh-read-trong-linux-3.jpg" width="765" /></div>

<figcaption>
<div style="text-align: justify;">Đọc lệnh với dấu phân cách Ifs</div>
</figcaption>
</figure>

<p style="text-align: justify;">Với biến IFS, bạn có thể nắm bắt nhiều đầu vào dòng từ người dùng và phân tách chúng dựa trên giá trị dấu phân cách.</p>

<h2 id="mcetoc_1id74q8ir17a" style="text-align: justify;">Thiết lập thời gian chờ khi đọc</h2>

<p style="text-align: justify;">Bạn cũng có thể cấu hình thời gian chờ cho lệnh read bằng tùy chọn<strong>&nbsp;-t</strong>. Điều này hữu ích nếu bạn muốn đảm bảo tập lệnh không phụ thuộc vào đầu vào của người dùng trong thời gian dài.</p>

<p style="text-align: justify;">Hãy thiết lập thời gian chờ 10 giây bằng tùy chọn&nbsp;<strong>-t</strong>:</p>

<pre id="pre13">
<code>read -t 10 -p &quot;Enter your name (10 seconds timeout): &quot; name 
&lt;user_input&gt;
echo &quot;Hello, $name!&quot;</code></pre>

<figure>
<div style="text-align: justify;"><img alt="Đọc giá trị đầu vào trong thời gian đã cho" data-i="11" data-src="https://st.quantrimang.com/photos/image/2024/11/21/lenh-read-trong-linux-1.png" data-was-processed="true" height="78" src="https://st.quantrimang.com/photos/image/2024/11/21/lenh-read-trong-linux-1.png" width="800" /></div>

<figcaption>
<div style="text-align: justify;">Đọc giá trị đầu vào trong thời gian đã cho</div>
</figcaption>
</figure>

<p style="text-align: justify;">Nếu người dùng không nhập bất kỳ thông tin gì trong vòng 10 giây, script sẽ tiếp tục thực thi.</p>

<h2 id="mcetoc_1id74q8ir17b" style="text-align: justify;">Cách ẩn đầu vào của người dùng</h2>

<p style="text-align: justify;">Đôi khi, bạn có thể muốn ẩn đầu vào của người dùng, chẳng hạn như khi nhập thông tin nhạy cảm như mật khẩu. Bạn có thể thực hiện điều này bằng cách sử dụng tùy chọn&nbsp;<strong>-s</strong>&nbsp;của lệnh read:</p>

<pre id="pre14">
<code>read -s -p &quot;Enter your password: &quot; password
&lt;user_input&gt;</code></pre>

<figure>
<div style="text-align: justify;"><img alt="Sử dụng tùy chọn -s với lệnh read để ẩn thông tin" data-i="12" data-src="https://st.quantrimang.com/photos/image/2024/11/21/lenh-read-trong-linux-2.png" data-was-processed="true" height="82" src="https://st.quantrimang.com/photos/image/2024/11/21/lenh-read-trong-linux-2.png" width="800" /></div>

<figcaption>
<div style="text-align: justify;">Sử dụng tùy chọn -s với lệnh read để ẩn thông tin</div>
</figcaption>
</figure>

<p style="text-align: justify;">Sau khi lưu trữ mật khẩu trong biến đã chỉ định, bạn có thể hiển thị mật khẩu đó trong terminal bằng lệnh echo:</p>

<pre id="pre15">
<code>echo -e &quot;\nPassword: $password&quot;</code></pre>

<figure>
<div style="text-align: justify;"><img alt="Hiển thị mật khẩu đã chụp bằng tùy chọn -e và lệnh echo" data-i="13" data-src="https://st.quantrimang.com/photos/image/2024/11/21/lenh-read-trong-linux-3.png" data-was-processed="true" height="133" src="https://st.quantrimang.com/photos/image/2024/11/21/lenh-read-trong-linux-3.png" width="691" /></div>

<figcaption>
<div style="text-align: justify;">Hiển thị mật khẩu đã chụp bằng tùy chọn -e và lệnh echo</div>
</figcaption>
</figure>

<p style="text-align: justify;">Ở đây, flag&nbsp;<strong>-e</strong>&nbsp;cho phép diễn giải các chuỗi thoát và chuỗi thoát&nbsp;<strong>\n</strong>&nbsp;thêm một dòng mới trước đầu ra.</p>

<p style="text-align: justify;">Khi bạn đọc xong bài viết này và làm theo các ví dụ, bạn sẽ quen với việc sử dụng lệnh read trong terminal Linux. Tuy nhiên, vẫn còn một số lệnh khác để khám phá. Hãy tiếp tục đọc để tìm hiểu&nbsp;các lệnh cơ bản dành cho người mới bắt đầu.</p>
		</div>
				<div id="author">
						<p>
				<strong>Nguồn tin:</strong>
				Quantrimang.com:
			</p>
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/cach-su-dung-lenh-read-trong-linux-9407.html" title="Cách sử dụng lệnh read trong Linux">https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/cach-su-dung-lenh-read-trong-linux-9407.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; .&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.
		</div>
		<div id="contact">
			<a href="mailto:admin@nguoicodonvn2008.info">admin@nguoicodonvn2008.info</a>
		</div>
	</div>
</div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="https://nguoicodonvn2008.info/#">Bấm vào đây để duy trì trạng thái đăng nhập</a>. Thời gian chờ: <span id="secField"> 60 </span> giây
        </div>
        <div id="openidResult" class="nv-alert" style="display:none"></div>
        <div id="openidBt" data-result="" data-redirect=""></div>
		</script>
		<div class="car-top">
  <span><img src="https://nguoicodonvn2008.info/themes/default/images/car.png" alt=""></span>
</div>
<div id="run_cronjobs" style="visibility:hidden;display:none;"><img alt="cron" src="/index.php?second=cronjobs&amp;p=r4uG5Zs2" width="1" height="1" /></div>
<script src="https://nguoicodonvn2008.info/assets/js/jquery/jquery.min.js"></script>
<script>var nv_base_siteurl="/",nv_lang_data="vi",nv_lang_interface="vi",nv_name_variable="nv",nv_fc_variable="op",nv_lang_variable="language",nv_module_name="news",nv_func_name="savefile",nv_is_user=0, nv_my_ofs=-4,nv_my_abbr="EDT",nv_cookie_prefix="nv4c_e856T",nv_check_pass_mstime=1738000,nv_area_admin=0,nv_safemode=0,theme_responsive=0,nv_recaptcha_ver=2,nv_recaptcha_sitekey="",nv_recaptcha_type="image",XSSsanitize=1;</script>
<script src="https://nguoicodonvn2008.info/assets/js/language/vi.js"></script>
<script src="https://nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js"></script>
<script src="https://nguoicodonvn2008.info/assets/js/global.js"></script>
<script src="https://nguoicodonvn2008.info/assets/js/site.js"></script>
<script src="https://nguoicodonvn2008.info/themes/default/js/news.js"></script>
<script src="https://nguoicodonvn2008.info/themes/default/js/main.js"></script>
<script src="https://nguoicodonvn2008.info/themes/default/js/custom.js"></script>
<script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "Organization",
            "url": "https://nguoicodonvn2008.info",
            "logo": "https://nguoicodonvn2008.info/uploads/angel.gif"
        }
        </script>
<script src="https://nguoicodonvn2008.info/themes/default/js/bootstrap.min.js"></script>
<script type="text/javascript">
var $scrolltop = $('.car-top');
$scrolltop.on('click', function () {
    $('html,body').animate({
        scrollTop: 0
    }, 800);
    $(this).addClass("car-run");
    setTimeout(function(){ $scrolltop.removeClass('car-run');}, 1000);
    return false;
});
$(window).on('scroll', function ()
{ 
    if($(window).scrollTop() >= 200)
    {
        $scrolltop.addClass("show");
        $scrolltop.addClass("car-down");
    }
    else
    {
       $scrolltop.removeClass("show");
       setTimeout(function(){ $scrolltop.removeClass('car-down');}, 300);
    }
});
</script>
</body>
</html>